0

I am using OpenSSL RSA1_5 for decrypting the CEK (Content Encryption Key).

My aim is to decrypt the JWK (JSON Web Key) by which I will be getting CEK, so by using CEK I can decrypt my ciphertext which is actually the encrypted data.

After using Base64Decode, JWE Header is

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

where "alg" is the algorithm used to decrypt the CEK. Please help me decrypting the CEK first, after this I need to decrypt the Cipher.

My Java class is:

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 am getting output as

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

Please help me to decrypt the CEK and solve this exception.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Vasu Dev
  • 11
  • 1
  • 1
  • 6

1 Answers1

1

Your problem is caused by your private key file. Firstly, your method of reading the bytes is error-prone:

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

This may not read the entire file. The available() method does not indicate how many bytes are in the file. Please search for a better way of reading this file (perhaps from this question: File to byte[] in Java).

Once this is fixed, you may still have errors unless your file is a DER-encoded PKCS #8 object. A common mistake is to try and use a PEM-encoded file (e.g. containing ----- BEGIN PRIVATE KEY ---- headers and base64-encoded data).

Community
  • 1
  • 1
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254