【问题标题】:Decrypt Content Encryption Key in JSON Web Encryption解密 JSON Web 加密中的内容加密密钥
【发布时间】:2014-03-27 19:30:54
【问题描述】:

我正在使用 OpenSSL RSA1_5 解密 CEK(内容加密密钥)。

我的目标是解密我将获得 CEK 的 JWK(JSON Web 密钥),因此通过使用 CEK,我可以解密我的密文,这实际上是加密数据。

使用Base64Decode后,JWE Header为

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

其中“alg”是用于解密 CEK 的算法。请先帮我解密 CEK,然后我需要解密 Cipher。

我的 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 并解决这个异常。

【问题讨论】:

    标签: java encryption jwe


    【解决方案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 编码的数据)。

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      相关资源
      最近更新 更多