使用 PHP 和 OpenSSL 生成对称密钥 AES-256-CBC



任何人都知道如何使用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();

AES加密密钥的随机生成与使用";openssl_random_pseudo_bytes"作用

下面是一个完整的AES 256 CBC加密字符串的运行示例,该字符串具有随机生成的密钥和IV。由于解密需要相同的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;
?>

最新更新