java - 解密 JSON Web 加密中的内容加密 key

标签 java encryption jwe

我使用 OpenSSL RSA1_5 来解密 CEK(内容加密 key )。

我的目标是解密 JWK(JSON Web key ),通过它我将获得 CEK,因此通过使用 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并解决此异常。

最佳答案

您的问题是由您的私钥文件引起的。首先,您读取字节的方法很容易出错:

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 ---- header 和 base64 编码数据)。

关于java - 解密 JSON Web 加密中的内容加密 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22679689/

相关文章:

c++ - 为什么我的代码打印出错误的密文?

linux - Linux 中的字符串加密

java - 如何将 OAEPParameterSpec 添加到 JWE 对象?

java - 我如何发现给定日期的季度?

java - 我是否需要扩展 ClassLoader 来重定向 Web 应用程序资源加载?

javascript - 我有 ascii 密码的编码功能,但需要有关如何解码它的帮助

node.js - 如何使用 node-jose 生成加密的 JWE

python - 如何加密字典数据?

java - Android Drawable getTransparentRegion() 不工作

java - 是否可以动态混合ListView和TextView?