Client side JavaScript encrypt using hybrid-crypto-js and decrypt using PHP OpenSSL fails

83 views Asked by At

I am unable to RSA decrypt in PHP Openssl (v1.1.1w), a cipher which was RSA encrypted using hybrid-crypto-js (v4.2.0).

I have verified that the key pair matches. They work individually e.g. in either JS or PHP.

Below is the code, is there anything I am missing here?

JavaScript:

const publicKey = "received-from-php";
const plainText = "my plain text here";
var crypt = new Crypt();
var encryptedText = crypt.encrypt(publicKey, plainText);
// send to php: JSON.parse(encryptedText).cipher

PHP:

$privateKey = file_get_contents("./private_key.pem");
$ret = openssl_private_decrypt(base64_decode($_GET['encrypted_text']), $decryptedText, $privateKey);
if (!$ret) echo "decryption failed";
else echo "Decrypted Text: " . ($decryptedText);

hybrid-crypto-js seem to support 2 padding schemes: RSA-OAEP, RSAES-PKCS1-V1_5. If I do not use OPENSSL_NO_PADDING in openssl_private_decrypt(), openssl_error_string() gives some errors/warnings. Kindly help.

0

There are 0 answers