Results 1 to 40 of 66

Thread: [VB6/VBA] Simple AES 256-bit password protected encryption

Threaded View

  1. #11

    Thread Starter
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,192

    Re: [VB6] Simple AES 256-bit password protected encryption

    Here is the equivalent php implementation with compatible arguments for the pbkdf2 key derivation

    Code:
    <?php
    const CIPHER = "AES-256-CTR";
    const KEYLEN = 32;
    const IVLEN = 16;
    const KDF_SALTLEN = 8;
    const KDF_ITER = 10000;
    const KDF_HASH = "SHA512";
    const OPENSSL_MAGIC = "Salted__";
    const OPENSSL_MAGICLEN = 8;
    
    function AesEncryptString($text, $password)
    {
        $salt = openssl_random_pseudo_bytes(KDF_SALTLEN);
        $derived = openssl_pbkdf2($password, $salt, KEYLEN + IVLEN, KDF_ITER, KDF_HASH);
        $encr = openssl_encrypt($text, CIPHER, substr($derived, 0, KEYLEN), OPENSSL_RAW_DATA, substr($derived, KEYLEN, IVLEN));
        return base64_encode(OPENSSL_MAGIC . $salt . $encr);
    }
    
    function AesDecryptString($encr, $password)
    {
        $encr = base64_decode($encr);
        $salt = "";
        if (substr($encr, 0, OPENSSL_MAGICLEN) == OPENSSL_MAGIC) {
            $salt = substr($encr, OPENSSL_MAGICLEN, KDF_SALTLEN);
            $encr = substr($encr, OPENSSL_MAGICLEN + KDF_SALTLEN);
        }
        $derived = openssl_pbkdf2($password, $salt, KEYLEN + IVLEN, KDF_ITER, KDF_HASH);
        return openssl_decrypt($encr, CIPHER, substr($derived, 0, KEYLEN), OPENSSL_RAW_DATA, substr($derived, KEYLEN, IVLEN));
    }
    
    $encr = AesEncryptString("this is a PHP test това е проба", "password123");
    echo $encr . "\n";
    echo AesDecryptString($encr, "password123") . "\n";
    ?>
    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width