为什么 mcrypt_encrypt returns 是一个不可读的字符串
Why mcrypt_encrypt returns a non readable character string
我尝试使用 16 位密钥加密数据,使用 ecb 模式 rinjndael_128 密码。加密成功,我也可以成功解密加密数据。但问题是,mcrypt_encrypt函数returns的字符串出现乱码。我想以十六进制格式查看此结果。
当我使用在线工具获取相同数据时得到这个十六进制值作为结果 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*
RIJNDAEL-128/ECB 的块大小是……128 位,即 16 字节。
但是您当前的输入是 32 字节,因此输出也是两个块,即 32 字节长。
您的明文看起来就像是 "hex-encoded" 的密钥。所以,把它当作钥匙。
此外 base64_encode() 与从字节序列生成 "hex-string" 不同。但是您可以为此使用 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
我尝试使用 16 位密钥加密数据,使用 ecb 模式 rinjndael_128 密码。加密成功,我也可以成功解密加密数据。但问题是,mcrypt_encrypt函数returns的字符串出现乱码。我想以十六进制格式查看此结果。
当我使用在线工具获取相同数据时得到这个十六进制值作为结果 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*
RIJNDAEL-128/ECB 的块大小是……128 位,即 16 字节。
但是您当前的输入是 32 字节,因此输出也是两个块,即 32 字节长。
您的明文看起来就像是 "hex-encoded" 的密钥。所以,把它当作钥匙。
此外 base64_encode() 与从字节序列生成 "hex-string" 不同。但是您可以为此使用 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