2
votes

I know this is a hard one... but I encrypt a file in a very standard way using OpenSSL. The file is encrypted in AES-256 using an RSA-2048 Public-Key. I want to decrypt the file in Java using the Private-Key. I researched for a long time and tried a lot of methods, but none seems to work. I just find related problems with working solutions, but not for exactly my problem.

I generate the Public-/Private-Key-Pair using this command:

openssl req -x509 -nodes -days 18250 -newkey rsa:2048
-keyout MyPrivateKey.pem -out MyPublicKey.pem
-subj "/C=CH/O=My Company/CN=My Key"

I encrypt the file using this command:

openssl smime -encrypt -aes256 -in message.zip -binary
-outform DEM -out message.dat MyPublicKey.pem

The file can be decrypted using this command:

openssl smime -decrypt -in message.dat -binary
-inform DEM -inkey MyPrivateKey.pem -out message.zip

But how can the decryption be done in Java? I know about JCE and I have heard of BouncyCastle. I just find hints, but no working solution.

BTW: It is a requirement and hard constraint that the file is asymmetrically encrypted using OpenSSL and decrypted using Java.

Thanks for your help, experts!

1

1 Answers

0
votes

The openssl command line creates CMS messages (specified in PKCS#7). You need the bouncy castle SMIME libraries to process those. The functionality is not in the standard Java API.