Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AES256 encryption in PHP and decryption in Delphi? #11

Open
pk0391 opened this issue Sep 17, 2020 · 1 comment
Open

AES256 encryption in PHP and decryption in Delphi? #11

pk0391 opened this issue Sep 17, 2020 · 1 comment

Comments

@pk0391
Copy link

pk0391 commented Sep 17, 2020

Hi,

i am trying to encrypt text in PHP (result of web form) and decrypt it in Delphi using AES-256, but withnout success.

In php i am using OpenSSL_encrypt with following code:

$key="secure";
$plaintext = "Hello";
$cipher = "aes-256-cbc";
$ivlen = 16;
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext = openssl_encrypt($plaintext, $cipher, $key, $options=0, $iv);

The Delphi code to decrypt is (i made my own function TEncUtil.DecryptString, which converts string to stream and then calls TEncUtil.Decrypt):

EncUtil.UseBase64 := true;
EncUtil.Passphrase := 'secure';
EncUtil.Cipher := 'AES-256-CBC';
EncUtil.DecryptString(cipherText, s);

When i am trying to decrypt it in Delphi, receive an error. Also the encrypted string length is different.
The problem i see is in initial vector, where in PHP is required also for decryption, but in Delphi is not required, but created during encryption (decryption?) process according some rules.

Do you have any suggestions to solve this problem?

Thanks in advance, Pavel

@AndrewKED
Copy link

It looks as if you are creating a random Initial Vector in your PHP. Then how would Delphi know what that initial vector is? Decryption will need to make use of both the same key AND the same initial vector that were used during the encryption process. If you're happy to do so, just start with a fixed value initial vector too (e.g. 16 0xFF bytes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants