用PHP&;生成符号键AES-256-CBC;OpenSSL

用PHP&;生成符号键AES-256-CBC;OpenSSL,php,openssl,aes,Php,Openssl,Aes,任何人都知道如何使用OpenSSL生成符号密钥AES-256-CBC。我使用openssl_random_pseudo_bytes函数生成IV。我尝试使用openssl\u加密,但它不起作用。 我在web上找到了它的实现,但是是用Java语言实现的 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES") ; keyGenerator.init(256); SecretKey secretKey = keyGene

任何人都知道如何使用OpenSSL生成符号密钥AES-256-CBC。我使用openssl_random_pseudo_bytes函数生成IV。我尝试使用openssl\u加密,但它不起作用。 我在web上找到了它的实现,但是是用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;
?>

谢谢,这对我帮助很大。请将我的回答标记为“已接受”、“谢谢”和“复活节快乐”:-)