Page 1 of 1

About encryption with AES

Posted: Thu Feb 10, 2022 6:24 am
by riku22
Hello.

I am trying to do AES encryption.
However, the encrypted string needs to be decrypted in PHP, and I don't know how to encrypt it in PureBasic.
I thought this page might be helpful, but I am not sure how to generate the key and IV.
The decryption in PHP is done as follows.

Code: Select all

<?php
$password = 'test';
$encrypted_text = $_POST["text"]; // Get base64 encoded text from POST request.
$salt = "1234567890";

$keyiv = openssl_pbkdf2($password, $salt, 32+16, 1000);
$key = substr($keyiv, 0, 32);
$iv = substr($keyiv, 32, 16);

$decrypted_text = openssl_decrypt($encrypted_text, 'AES-256-CBC', $key, 0, $iv);
echo $decrypted_text;
?>
I would appreciate any advice you can give me.

Re: About encryption with AES

Posted: Thu Feb 10, 2022 11:35 am
by Caronte3D
I was used this module some time ago.
Works nice now after correct the a bug I was found
I pass the compresed string and the IV to the php script

I don't have access to my source, so I can't tell you more, but you can begin with that module.
https://www.purebasic.fr/german/viewtop ... 92#p309907

Re: About encryption with AES

Posted: Fri Feb 11, 2022 1:20 pm
by riku22
Hello.

Thank you.
I tried a bit of the script mentioned in the link, but it still doesn't seem to decrypt in PHP.
I would like to avoid passing the IV to PHP if possible.
It seems to be quite difficult for me.

Sincerely yours.

Re: About encryption with AES

Posted: Fri Feb 11, 2022 1:47 pm
by Rinzwind
Side note: Php uses utf8 for text. So make sure to transform the utf16 string of pb to utf8 before encrypting. Or the other way around, agree on text encoding.

Re: About encryption with AES

Posted: Fri Feb 11, 2022 4:12 pm
by Caronte3D
riku22 wrote: Fri Feb 11, 2022 1:20 pm Hello.

Thank you.
I tried a bit of the script mentioned in the link, but it still doesn't seem to decrypt in PHP.
I would like to avoid passing the IV to PHP if possible.
It seems to be quite difficult for me.

Sincerely yours.
The AES module I mentioned can be decoded Ok with PHP (I do that some time ago as I already said)

You *must* pass the IV (different each time) to the PHP script in order to use CBC in a secure way: https://en.wikipedia.org/wiki/Block_cip ... ector_(IV)

Notice you must encode base64 prior to send to php because cripted info may containt data outside of the string representation.

Re: About encryption with AES

Posted: Tue Feb 15, 2022 4:33 am
by riku22
Hello.

Hmm, I see.
Thank you.