Skip to content
Advertisement

Sage Pay / Opayo Form integration – replacing mcrypt with openssl

We use the Sage Pay / Opayo form integration to pass customer orders to Sage Pay for payment, then to process the report that comes back. Sage Pay’s example code relies on the PHP mcrypt functions which are no longer supported from PHP 7.2, so we need to update the scripts to OpenSSL.

Encryption was covered by a great post here: Upgrade mcrypt to OpenSSL encryption in SagePay form

  • this works fine for me:
JavaScript

…but I can’t find a matching code to fix the decrypt function.

The current code is:

JavaScript

Could anyone help with an OpenSSL version, please?

Advertisement

Answer

When decrypting, the @ prefix must first be removed, then hex decoding must be done, and finally decryption can be performed. openssl_decrypt() implicitly removes the padding, so that no explicit removal is required. This is implemented in decryptAes_new() below:

JavaScript

which produces the output:

JavaScript

Checking the length of the plaintext after decryption, 43 bytes, shows that the padding has been automatically removed.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement