PB AES vs php AES
Posted: Wed Aug 14, 2019 4:04 pm
I want to exchange some text between PB app and php file. In PB, I pass the string from AES and then from Base64, with the following code:
Then, I copy paste the result (XbwBpBOPncqGq8vOVuikRfoiQQrC74ymULEa4pQizgI=) and try to decrypt it in php:
The string is not decrypted. I also tried the opposite, the string encrypted from the php and PB fails to decrypt it. I did some tests, tried to do the same without the use of AES, just base64, and the string decoded just fine. So, what is wrong? Key, iv, 256 bits, cbc mode, all these the same. Are they different AES approaches?
Code: Select all
InitNetwork()
key.s = "12345678901234567890123456789012"
InitializationVector.s = "0987654321098765"
String$ = "string coming from the client..."
lenth.i = StringByteLength(String$,#PB_Ascii)
*string = AllocateMemory(lenth+1,#PB_Memory_NoClear)
Debug PokeS(*string,String$,lenth,#PB_Ascii)
StringMemorySize.i = lenth + 1; Space for the string and its null terminating character
*CipheredString = AllocateMemory(StringMemorySize,#PB_Memory_NoClear)
*DecipheredString = AllocateMemory(StringMemorySize,#PB_Memory_NoClear)
outsize.i = StringMemorySize*3; for Base64
*str = AllocateMemory(outsize,#PB_Memory_NoClear); for Base64
If AESEncoder(*string, *CipheredString, lenth, @Key, 256, @InitializationVector,#PB_Cipher_CBC)
Debug StringByteLength(String$)
Debug MemorySize(*CipheredString)
Debug StringMemorySize
i = Base64EncoderBuffer(*CipheredString, lenth,*str,outsize)
Debug "base64 = " + i
Debug "str = " + PeekS(*str,i,#PB_Ascii)
Debug "str = " + PeekS(*str,i,#PB_UTF8)
Debug "str = " + PeekS(*str,i,#PB_Unicode)
*Decoded = AllocateMemory(1024)
Debug "decode = " + Base64DecoderBuffer(@cmd,StringByteLength(cmd),*Decoded, 1024)
i=AESDecoder(*Decoded, *DecipheredString, lenth, @Key, 256, @InitializationVector)
Debug "Deciphered: "+PeekS(*DecipheredString,i,#PB_UTF8)
Debug "Deciphered: "+PeekS(*DecipheredString,i,#PB_Unicode)
Debug "Deciphered: "+PeekS(*DecipheredString,i,#PB_Ascii)
EndIf
Debug "ok"
Code: Select all
<?php
function fnDecrypt($sValue, $key, $iv) {
$cipher="AES-256-CBC";
return openssl_decrypt($sValue, $cipher, $key, 0, $iv);
}
$key = "12345678901234567890123456789012";
$iv = "0987654321098765";
$str = "/F/9WXb7y6j1tOcKTmFy608wO2BFo8jZ1HUo4+oPUG9AQ/UyY1KxVcVbNfDtRNAQdIqseKNuK5DPpkhooAxVBAAA";
$newStr = fnDecrypt($str, $key, $iv);
echo "<br/>str = " . $str;
echo "<br/>new = " . $newStr;
?>