B4J Question AES Encryption/Decryption

ThRuST

Well-Known Member
Licensed User
Longtime User
Hi! I'm looking for a solution to use AES algorithm in my project and decided to post a question about it.
As this concerns B4J I posted it here, but if it's compatible with B4X it would be great.

I found this page as a reference, you'll find it here

Best regards,
Roger
 

jahswant

Well-Known Member
Licensed User
Longtime User
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@jahswant I've never seen that before, I'll check it out more closely. Thanks

Here's a solution for PHP by @QtechLab šŸ‘

AES:
<?PHP

$data = "JlTf/Y6JTO6dXUbYrEcCuQ==";
$key = "Salted_key_is_here";
$test = decrypt256($data, $key);

echo "Result: " . $test;


function encrypt256($data, $key)
{
    $method = 'aes-256-cbc';
    
    // Must be exact 32 chars (256 bit). Salt32 make a robust hash around the entered key
    $password = $key; //salt32($key);
    
    // IV must be exact 16 chars (128 bit) Example with zeros
    $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
    //
    $encrypted = base64_encode(openssl_encrypt($data, $method, $password, OPENSSL_RAW_DATA, $iv));
    
    return $encrypted;
}

function decrypt256($dataEncrypted, $key)
{
    $method = 'aes-256-cbc';
    
    // Must be exact 32 chars (256 bit). Salt32 make a robust hash around the entered key
    $password = $key; //salt32($key);
    
    // IV must be exact 16 chars (128 bit)  Example with zeros
    $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
    //
    $decrypted = openssl_decrypt(base64_decode($dataEncrypted), $method, $password, OPENSSL_RAW_DATA, $iv);
 
    return $decrypted;
}

?>
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
I found that there's jB4XEncryption library buildt into the IDE. It would be nice to use that since it's compatible with all B4X tools. Anyone?
The post is from 2014 but it's still in the IDE.

Here's the thread
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Erel I am evaluating a cross platform encryption/decryption solution for my project.
Then I noticed B4XEncryption library in the IDE. Does that handle AES for all B4X IDE's? I want to encrypt/decrypt a file and save it back.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Erel Should we use the example from this >post<, or is there an updated version? Perhaps it's been documented in the Libraries section.
It depends on something called BouncyCastle :)
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
@Erel I downloaded the file BouncyCastle and noticed it already was part of B4J v9.50 which I currently use. I'll investigate this more deeply.
B4XEncryption seems the way to go. Thanks

1656427892267.png
 
Upvote 0
Top