我尝试使用16位密钥使用ecb模式为rinjndael_128密码加密数据。加密成功,我也可以成功解密加密数据。但问题是,mcrypt_encrypt函数返回乱码字符串。我希望以十六进制格式看到这个结果。
当我使用在线工具获取相同数据时,将此十六进制值作为结果bd61ce515890e2e3fb5e404bbe886cc2
码
$key = pack('H*', "07070609070306050601070007000700");
$plaintext = "2dfb0998b2f76f35f5b08972b57cfbfc";
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
$ciphertext_base64 = base64_encode($ciphertext);
echo "encrypted - text: ".$ciphertext . "<br>";
echo "encrypted base 64 encoded - text: ".$ciphertext_base64 . "<br>";
$ciphertext_dec = base64_decode($ciphertext_base64);
$plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_ECB);
echo "decrypted data - text: ".$plaintext_dec . "<br>";
结果:
before encryption - text: 2dfb0998b2f76f35f5b08972b57cfbfc
encrypted - text: (MÓx‹ÓåBÖ i½4²5žNUXÃè/Óë£@ö
encrypted base 64 encoded - text: KE3TeIsa0+VC1g1pCL00sjWeTlVYw+iNgS/TEOujQPY=
decrypted data - text: 2dfb0998b2f76f35f5b08972b57cfbfc*
答案 0 :(得分:1)
RIJNDAEL-128 / ECB的块大小是......好128位,即16字节 但是您的输入当前是32个字节,因此输出也是两个块,即32个字节长。
你的明文看起来就像钥匙一样是“十六进制编码”。所以,把它当成钥匙 base64_encode()与从字节序列生成“十六进制字符串”不同。但是你可以使用unpack()。
<?php
$key = pack('H*', '07070609070306050601070007000700');
$plaintext = pack('H*', '2dfb0998b2f76f35f5b08972b57cfbfc');
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
list(,$ciphertext_hex) = unpack('H*', $ciphertext);
echo $ciphertext_hex;
打印bd61ce515890e2e3fb5e404bbe886cc2