2014-03-27 13 views
0

CEK (콘텐츠 암호화 키)를 해독하는 데 OpenSSL RSA1_5를 사용하고 있습니다.JSON 웹 암호화에서 콘텐츠 암호화 키 암호 해독

제 목표는 CEK를 얻는 JWK (JSON Web Key)를 해독하는 것입니다. 따라서 CEK를 사용하여 실제로 암호화 된 데이터 인 암호문을 해독 할 수 있습니다.

Base64Decode는 사용 후 JWE 헤더 "ALG"은 CEK를 해독하는데 사용되는 알고리즘이다

{"alg":"RSA1_5","enc":"A128CBC-HS256","typ":"JOSE"} 

이다. 이 암호를 해독해야만 CEK 암호 해독을 도와주세요.

내 자바 클래스는 다음과 같습니다

package com.decryption; 

import java.io.*; 
import java.math.BigInteger; 

import java.security.*; 
import java.security.spec.*; 
import java.security.interfaces.*; 

import javax.crypto.*; 
import javax.crypto.spec.*; 
import javax.crypto.interfaces.*; 

public class RSADecrypt 
{ 
    public RSADecrypt(String inFileName, String outFileName) { 


     try { 
      System.out.println("Inside TRY"); 
     /* Get the encrypted message from file. */ 
     FileInputStream cipherfile = new FileInputStream(inFileName); 
     byte[] ciphertext = new byte[cipherfile.available()]; 
     cipherfile.read(ciphertext); 
     cipherfile.close(); 
     System.out.println("Inside 1"); 
     /* Get the private key from file. */ 
     //PrivateKey privatekey = readPrivateKey("D://sso//mmdevnopass.key"); 
     PrivateKey privatekey = readPrivateKey("D://sso//mmdevJWE.key"); 
     System.out.println("Inside 2"); 

     /* Create cipher for decryption. */ 
     Cipher decrypt_cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
     decrypt_cipher.init(Cipher.DECRYPT_MODE, privatekey); 
     System.out.println("Inside 3"); 
     /* Reconstruct the plaintext message. */ 
     byte[] plaintext = decrypt_cipher.doFinal(ciphertext); 
     FileOutputStream plainfile = new FileOutputStream(outFileName); 
     plainfile.write(plaintext); 
     plainfile.close(); 
     } catch (Exception e) { 
      System.out.println("catch1"); 
     e.printStackTrace(); 
     } 
    } 

    public static PrivateKey readPrivateKey(String filename) throws Exception { 
     System.out.println("readPrivateKey()"); 
     FileInputStream file = new FileInputStream(filename); 
     byte[] bytes = new byte[file.available()]; 
     file.read(bytes); 
     file.close(); 
     System.out.println("readPrivateKey() 1"); 
     PKCS8EncodedKeySpec privspec = new PKCS8EncodedKeySpec(bytes); 
    // X509EncodedKeySpec privspec= new X509EncodedKeySpec(bytes); 
     //RSAPrivateKeySpec privspec = new RSAPrivateKeySpec(modulus, privateExponent) 
     System.out.println("readPrivateKey() 2"); 
     KeyFactory factory = KeyFactory.getInstance("RSA"); 
     System.out.println("readPrivateKey() 3"); 
     PrivateKey privkey = factory.generatePrivate(privspec); 
     System.out.println("readPrivateKey() 4"); 
     return privkey; 
    } 

    public static void main(String[] arg) { 
     /*if (arg.length != 2) { 
     System.err.println("Usage: java RSADecrypt <src file> <dest file>"); 
     } else {*/ 
     System.out.println("Welcome"); 
     String inFileName="D://sso//myJEK.txt"; 
     String outFileName="D://sso//out.txt"; 
     new RSADecrypt(inFileName,outFileName); 
    // } 
    } 
} 

I 출력을 얻고있다

Welcome 
Inside TRY 
Inside 1 
readPrivateKey() 
readPrivateKey() 1 
readPrivateKey() 2 
readPrivateKey() 3 
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format 
    at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:175) 
    at java.security.KeyFactory.generatePrivate(KeyFactory.java:322) 
    at com.decryption.RSADecrypt.readPrivateKey(RSADecrypt.java:85) 
    at com.decryption.RSADecrypt.<init>(RSADecrypt.java:46) 
    at com.decryption.RSADecrypt.main(RSADecrypt.java:102) 
Caused by: java.security.InvalidKeyException: invalid key format 
    at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:324) 
    at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:350) 
    at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:74) 
    at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:58) 
    at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:274) 
    at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:171) 
    ... 4 more 
catch1 

로 나를 CEK를 해독하는 데 도움이 예외를 해결하십시오.

답변

1

개인 키 파일로 인해 문제가 발생했습니다. 먼저 바이트를 읽는 방법이 오류가 발생하기 쉽습니다.

FileInputStream file = new FileInputStream(filename); 
byte[] bytes = new byte[file.available()]; 
file.read(bytes); 
file.close(); 

전체 파일을 읽을 수 없습니다. available() 메서드는 이 아니며은 파일에 몇 바이트가 있는지 나타냅니다. 이 파일을 더 잘 읽을 수있는 방법을 찾으십시오 (아마도이 ​​질문에서 : File to byte[] in Java).

일단이 문제가 해결되면 파일이 DER로 인코딩 된 PKCS # 8 개체가 아니면 오류가 발생할 수 있습니다. 일반적인 실수는 PEM 인코딩 파일 (예 : ----- BEGIN PRIVATE KEY ---- 헤더 및 base64 인코딩 데이터 포함)을 사용하고 시도하는 것입니다.