Rijndael 256 谈 PHP 加密困难

发布于 2024-12-26 20:35:27 字数 947 浏览 2 评论 0原文

我必须传递一个值并将其加密为以下规范:

“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2025-01-02 20:35:27

如果您的要求是 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) ?

甜嗑 2025-01-02 20:35:27

这已经解决了......(un?)幸运的是我只是得到了一个错误的参数。

This has been solved... (un?)fortunately I was simply given a wrong parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文