About encryption with AES

Everything else that doesn't fall into one of the other PB categories.
riku22
New User
New User
Posts: 8
Joined: Wed Feb 09, 2022 3:20 am

About encryption with AES

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: About encryption with AES

Post 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
riku22
New User
New User
Posts: 8
Joined: Wed Feb 09, 2022 3:20 am

Re: About encryption with AES

Post 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.
Rinzwind
Enthusiast
Enthusiast
Posts: 690
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: About encryption with AES

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: About encryption with AES

Post 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.
Last edited by Caronte3D on Tue Feb 15, 2022 9:11 am, edited 1 time in total.
riku22
New User
New User
Posts: 8
Joined: Wed Feb 09, 2022 3:20 am

Re: About encryption with AES

Post by riku22 »

Hello.

Hmm, I see.
Thank you.
Post Reply