Rijndael 256 谈 PHP 加密困难
我必须传递一个值并将其加密为以下规范:
“256 位密钥长度、256 位块长度、32 字节块、ECB 模式、 ASCII 编码(加密数据预计以字符串形式提供 每个字符都已转换为 2 字节的十六进制值)”
但是我一定错过了一些东西。这是出于网络服务的验证目的,但由于解密失败,我一直被拒绝。
这就是我所拥有的:
$key = '1324mykey';
$string = 'Ron Swanson';
// 1. Encrypt the string with the key using Rijndael 256 in ECB mode
$td = mcrypt_module_open('rijndael-256', '','ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_string = mcrypt_generic($td, $string);
// 2. Base64 encode my string
$encrypted_string = base64_encode($encrypted_string);
// 3. Convert each character of encrypted_string to it's 2-byte HEX value
$hex='';
for ($i=0; $i < strlen($encrypted_string); $i++)
{
$hex .= dechex(ord($encrypted_string[$i]));
}
// Now $encrypted_string should match up with the recipe, but it isn't.
$encrypted_string = $hex;
我希望我在流程的第 1 步中遗漏了一些基本的加密要求。
I have to pass a value and have it encrypted to the following spec:
"256-bit key length, 256-bit block length, 32-byte blocks, ECB mode, with
ASCII encoding (encrypted data is expected to be provided as a string
with each character having been converted to a 2-byte HEX value)"
However I must be missing something. This is for verification purposes with a webservice but I keep getting rejected due to a decryption failure.
Here's what I have:
$key = '1324mykey';
$string = 'Ron Swanson';
// 1. Encrypt the string with the key using Rijndael 256 in ECB mode
$td = mcrypt_module_open('rijndael-256', '','ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_string = mcrypt_generic($td, $string);
// 2. Base64 encode my string
$encrypted_string = base64_encode($encrypted_string);
// 3. Convert each character of encrypted_string to it's 2-byte HEX value
$hex='';
for ($i=0; $i < strlen($encrypted_string); $i++)
{
$hex .= dechex(ord($encrypted_string[$i]));
}
// Now $encrypted_string should match up with the recipe, but it isn't.
$encrypted_string = $hex;
I'm hoping there's something basic in the encryption requirements that I'm missing in my step 1 of my process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的要求是 256 位密钥,那么您的
$key
变量不应该是 32 字节吗? (换句话说,32 个字符长)?If your requirement is a 256-bit key, shouldn't your
$key
variable be 32 bytes? (in other words, 32 characters long) ?这已经解决了......(un?)幸运的是我只是得到了一个错误的参数。
This has been solved... (un?)fortunately I was simply given a wrong parameter.