Decrypt PHP encrypted string (does anyone use the Crypto library?)

Hi, I’d like to decrypt, on my OpenFL app a string encryped on PHP. At the server, I’m using “openssl_encrypt” like this:

$encrypted = openssl_encrypt($original, ‘AES-128-CTR’, $encKey, 0, $encVec);

At my OpenFL project, I’m using the Haxe Crypto library (GitHub - HaxeFoundation/crypto: Cross platform cryptographic functions for Haxe), following their AES decryption example:

var aes:Aes = new Aes();
var key = Bytes.ofString(encKey);
var iv:Bytes = Bytes.ofString(encVec);
aes.init(key, iv);
var data = Bytes.ofString(‘my encrypted string from php’);
trace(aes.decrypt(Mode.CTR, data, Padding.NoPadding));

This, however, does not work. Does anyone here use this Crypto library to tell me how to handle this?

Never tried the library, but have you tried to encrypt and decrypt with the library only to see if it works properly?
Also, you can compare encrypted string from PHP and haxe library.

The most probably your problem is about key and iv and using Bytes.ofString and not Bytes.ofHex.

The method Bytes.ofString could have a problem to convert some characters to correct representation in Bytes .
For that reason is more reliable to use Bytes.ofHex .

The other thing is to check $encKey and $encVec with encKey and encVec in hex code.
Probably they are different.

Also , you set option = 0 for padding in openssl_encrypt , but no padding option ( OPENSSL_ZERO_PADDING ) is 2 . ( check here : PHP: Other Constants - Manual )
From PHP Doc:
Without using OPENSSL_ZERO_PADDING, you will automatically get PKCS#7 padding.

Maybe you want to use padding in your Haxe code ? In that case set Padding.PKCS7

1 Like

Thank you! I believe the problem wasn’t on “ofString” - the results were the same when using hex versions of the key/iv (I believe I wasn’t using any problematic char), but the padding was indeed messing up with the decodig. Thank you a lot!