【问题标题】:Generation of symetric key AES-256-CBC with PHP & OpenSSL使用 PHP 和 OpenSSL 生成对称密钥 AES-256-CBC
【发布时间】:2021-04-03 07:46:26
【问题描述】:

任何人都知道如何使用 OpenSSL 生成对称密钥 AES-256-CBC。我使用 openssl_random_pseudo_bytes 函数生成 IV。我尝试使用 openssl_encrypt - 但它不起作用。 我在网上找到了它的实现,但是用的是 Java 语言。

KeyGenerator keyGenerator = KeyGenerator.getInstance("AES") ;
keyGenerator.init(256);
SecretKey secretKey = keyGenerator.generateKey() ;
return secretKey.getEncoded();

【问题讨论】:

    标签: php openssl aes


    【解决方案1】:

    AES 加密密钥的随机生成与使用“openssl_random_pseudo_bytes”函数的初始化向量完全相同。

    您可以在下面找到一个完整运行的示例,该示例使用随机生成的密钥和 IV 对字符串进行 AES 256 CBC 加密。由于您需要相同的 IV 进行解密,因此输出为 (Base64 编码) IV : (Base64 编码) 密文 - 在实际程序中,您将在编码之前以二进制方式连接两者。

    这是输出:

    AES CBC 256 String encryption with random key full
    plaintext: The quick brown fox jumps over the lazy dog
    encryptionKey (Base64): wJknn3c1MHWhDQaqCVYU648EbkdxIVjWghiJoXrpva8=
    
    * * * Encryption * * *
    ciphertext: s7L1BK/ds97P+abbuHtNUA==:5hWlNtTdEUgjQTkOVWV/QG2RauFjXNWwr+2Dczgx55pTg4azuNhwZyTFhyYFQSVh
    output is (Base64) iv : (Base64) ciphertext
    
    Cross platform cryptography: AES CBC 256 String encryption with random key (PHP)
    
    * * * Decryption * * *
    decryptionKey (Base64): JJu2xyi4sP7lTfxVi8iPvKIIOWiwkgr7spyUEhsnjek=
    ciphertextDecryption (Base64): +KOM4ASOY0cMNrM9zV/qvw==:HuoyDiusSplYfd04XSNLOqtcWyOFwmeHNzXS5ywmqRkgAXi8do/6dKppo2U3ZoKl
    input is (Base64) iv : (Base64) ciphertext
    plaintext: The quick brown fox jumps over the lazy dog
    

    请注意,代码没有异常处理,仅用于教育目的。

    <?php
    function generateRandomAesKey()
    {
        return openssl_random_pseudo_bytes(32, $crypto_strong);
    }
    
    function generateRandomInitvector()
    {
        return openssl_random_pseudo_bytes(16, $crypto_strong);
    }
    
    function base64Encoding($input)
    {
        return base64_encode($input);
    }
    
    function base64Decoding($input)
    {
        return base64_decode($input);
    }
    
    function aesCbcEncryptToBase64($key, $data)
    {
        $iv = generateRandomInitvector();
        $ciphertext = openssl_encrypt($data, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
        return base64_encode($iv) . ':' . base64_encode($ciphertext);
    }
    
    function aesCbcDecryptFromBase64($key, $data)
    {
        list($iv, $encryptedData) = explode(':', $data, 2);
        return openssl_decrypt(base64_decode($encryptedData), 'aes-256-cbc', $key, OPENSSL_RAW_DATA, base64_decode($iv));
    }
    
    echo 'AES CBC 256 String encryption with random key full' . PHP_EOL;
    
    $plaintext = 'The quick brown fox jumps over the lazy dog';
    echo 'plaintext: ' . $plaintext . PHP_EOL;
    
    // generate random key
    $encryptionKey = generateRandomAesKey();
    $encryptionKeyBase64 = base64Encoding($encryptionKey);
    echo 'encryptionKey (Base64): ' . $encryptionKeyBase64 . PHP_EOL;
    
    // encryption
    echo PHP_EOL . '* * * Encryption * * *' . PHP_EOL;
    $ciphertextBase64 = aesCbcEncryptToBase64($encryptionKey, $plaintext);
    echo 'ciphertext: ' . $ciphertextBase64 . PHP_EOL;
    echo 'output is (Base64) iv : (Base64) ciphertext' .PHP_EOL;
    
    echo PHP_EOL;
    echo 'Cross platform cryptography: AES CBC 256 String encryption with random key (PHP)' . PHP_EOL;
    // decryption
    echo PHP_EOL . '* * * Decryption * * *' . PHP_EOL;
    $decryptionKeyBase64 = $encryptionKeyBase64;
    $ciphertextDecryptionBase64 = $ciphertextBase64;
    echo 'decryptionKey (Base64): ' . $decryptionKeyBase64 . PHP_EOL;
    
    echo 'ciphertextDecryption (Base64): ' . $ciphertextDecryptionBase64 . PHP_EOL;
    echo 'input is (Base64) iv : (Base64) ciphertext' .PHP_EOL;
    $decryptionKey = base64Decoding($decryptionKeyBase64);
    $decryptedtext = aesCbcDecryptFromBase64($decryptionKey, $ciphertextDecryptionBase64);
    echo 'plaintext: ' . $decryptedtext . PHP_EOL;
    ?>
    

    【讨论】:

    • 请将我的回答标记为“已接受”,谢谢和“复活节快乐”:-)
    猜你喜欢
    • 2013-08-11
    • 1970-01-01
    • 2013-03-13
    • 2017-08-28
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    相关资源
    最近更新 更多