Java code to decrypt OpenSSL RSA Encrypted data

Java code to decrypt OpenSSL RSA Encrypted data


Job Description

I have a Java class that takes a RSA Public Key and decrypts data encrypted by an RSA Private Key.

The encrypted data was encrypted by Ruby:

require 'openssl'
require 'base64'
pkey =
privateKey = pkey.to_s
publicKey = pkey.public_key.to_s
decryptionKey = Base64.encode64(pkey.private_encrypt('{"key":"Hatch-Polish8impact","iv":"wCSgmiWq/Sgsr4D4D0inIQ=="}')).strip().gsub("\n", "")

When decrypting with Java, the decryption method returns the error: javax.crypto.BadPaddingException: Data must start with zero.

Ruby's OpenSSL PrivateKey Encryption uses PKCS1_PADDING by default:

We are using Ruby 2.0 which encodes the RSA Key in X.509 format:

I need Java code that can decrypt the provided OpenSSL Encrypted reference data. The toBeDecrypted string will be decrypted using the publicKey to create the plaintext string:

String publicKey = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/WTuhE2XrvtHfOaZWaN2hpwh0\nqdtQKCZjooo1ogiORSJxvDhE3+dxb0UkL5xdztZekmJqasvWksxSiVBLmgm4qMHs\nEng32Q27CQykMTsWedmbhColxvK+5ZlmhK+EwBXCyLwmCNIEfWqAtsy9vroP0Zew\nHggozcYKrCoYsGQR8wIDAQAB\n-----END PUBLIC KEY-----";

String toBeDecrypted = "ZyG5Hu09BGB3xpuDyLAIMlFyTwKcBsjXQAiUdT4gjxeXpAX68zq4LgSGLm5SGN6JD4i8GaTLzUHWaiOeor0VbmGdJX3GFu8d5vDQL4I7QEq87SLiVpHjlMZMTjoqT3M5rFEcsAQnxX1SKVi33xc9YfPcqvZ0Y6Q9QGi1eZGHmCI=";

String plaintext = '{"key":"Hatch-Polish8impact","iv":"wCSgmiWq/Sgsr4D4D0inIQ=="}';

The attached zip file has a screenshot of the error and the code we've written so far that needs to be modified.

If you want to use external libraries, like BouncyCastle, please mention as much in your proposal.

I need to get this solution in place by Friday, December 6th, so a quick turn-around will be required.

Open Attachment