2014-03-27 132 views
0

我正在使用OpenSSL RSA1_5解密CEK(內容加密密鑰)。解密JSON Web加密中的內容加密密鑰

我的目標是解密我將獲得CEK的JWK(JSON Web Key),所以通過使用CEK,我可以解密我的密文,這實際上是加密數據。

使用Base64Decode後,JWE部首是

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

其中 「ALG」 是用於解密CEK的算法。請幫我先解密CEK,之後我需要解密密碼。

我的Java類是:

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); 
    // } 
    } 
} 

我得到輸出

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編碼數據)。