我使用AES ALgo和128位CBC密码模式加密,下面是代码:
$cc = 'my secret text';
$key = '3aa22e01c04c7059778c54d122b0273689fba00f4a166a66d15f7ba6a8ba8743';
$iv = '1234567890123456';
$length = strlen($cc);
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);
echo "encrypted: " . $encrypted;
echo "\n";
echo "decrypted: " . substr($decrypted, 0, $length) . "\n";
但是这给了我一个警告,输出为:
警告:mcrypt_generic_init():密钥大小太大;提供长度: 64,最大:32在/var/www/cipher.php第10行警告: mcrypt_generic_init():密钥大小太大;提供长度:64,最大: 第14行的/var/www/cipher.php中的32加密: vM / XVYSjs / QApdCUEQ8bdQ ==解密:我的秘密文本
现在有人可以指导我为什么我面临尺寸问题以及如何在不改变密钥的情况下消除尺寸问题......
答案 0 :(得分:0)
您正在进行128位加密,但您的密钥长度为64个十六进制字符,即265位。
您需要进行256位加密,或者您的密钥需要一半大小。
答案 1 :(得分:0)
我修改你的代码并运行正常, 请检查我的更改,确定
$data_to_encrypt = "2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~";
$key128 = "abcdef0123456789abcdef0123456789";
$iv = "0000000000000000";
$cc = $data_to_encrypt;
$key = $key128;
$iv = $iv;
$length = strlen($cc);
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);
echo "encrypted: " . $encrypted;
echo "<br/>";
echo "length:".strlen($encrypted);
echo "<br/>";
echo "decrypted: " . substr($decrypted, 0, $length);