在我们的项目中,我必须使用AES 128 CBC算法以及以下数据:
KEY =“abcdef0123456789abcdef0123456789”
IV =“00000000000000000000000000000000”
要加密的文字是
2〜1〜000024〜0910〜20130723092446〜T〜00002000〜USD〜F〜375019001012120〜0〜0〜000亿〜
预期的结果是
0D 58 35 AF EB EE 04 C6 DC 24 21 53 8D B7 C3 8A 12 83 97 0E B3 1F 21 A4 7D 2E 3C C6 23 D2 9E F0 46 12 79 C7 AC F9 3B 03 1B E2 B6 9C E4 5C 93 39 55 49 57 F2 9E F6 09 F0 19 EE C9 75 98 3A 03 B5 37 62 2D 7E 0F 19 6B E1 48 F1 C7 CB B8 8E 60 2A
结果文本的长度为160个字符。
我尝试使用了很多算法,但我的所有结果都不同。
您是否会使用自己的方法加密文本并使用预期结果进行验证?
您会粘贴结果吗?
这是我的测试:
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$data_to_encrypt = "2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~";
$key128 = "abcdef0123456789abcdef0123456789";
$iv = "0000000000000000"."0000000000000000";
echo "data_to_encrypt: ".$data_to_encrypt."<br/>";
if (($res = mcrypt_generic_init($cipher, $key128, $iv)) != -1)
{
// PHP pads with NULL bytes if $cleartext is not a multiple of the block size.
$cipherText = mcrypt_generic($cipher,$data_to_encrypt);
mcrypt_generic_deinit($cipher);
$data_encrypted = strtoupper(bin2hex($cipherText));
echo "result encrypt: ".$data_encrypted."<br/>";
echo "len: ".strlen($data_encrypted)."<br/>";
}
这是我的答案:
data_to_encrypt:2~1~000024~0910~20130723092446~T~000000~USD~F~375019001012120~0~0~00000000000~ 结果加密:C9A4E600BAC6FC4F2077FF62DDB5F9BEAB1C9C567A98E6B81373711A4BC0FFE1748F5E26886896AB87CD375567C5466E6A9F4C079AD19B592E080896BCFDB52324F342A9F94AE71FADB1F6820EB57F7B len:160
提前感谢您的帮助和时间。
答案 0 :(得分:2)
这个C程序给出了正确的结果:
#include <stdio.h>
#include <openssl/aes.h>
int main()
{
unsigned char key[16] = {0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,0x89};
unsigned char iv[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char plaintext[] = {
'2','~','1','~','0','0','0','0','2','4','~','0','9','1','0','~',
'2','0','1','3','0','7','2','3','0','9','2','4','4','6','~','T',
'~','0','0','0','0','2','0','0','0','~','U','S','D','~','F','~',
'3','7','5','0','1','9','0','0','1','0','1','2','1','2','0','~',
'0','~','0','~','0','0','0','0','0','0','0','0','0','0','0','~',
};
unsigned char ciphertext[sizeof(plaintext)];
AES_KEY aes;
size_t i;
AES_set_encrypt_key(key,128,&aes);
AES_cbc_encrypt(plaintext,ciphertext,sizeof(plaintext),&aes,iv,AES_ENCRYPT);
for(i=0; i<sizeof(ciphertext); i++) {
printf("%02x ", ciphertext[i]);
}
printf("\n");
return 0;
}
gcc -o test test.c -lcrypto
./test
0d 58 35 af eb ee 04 c6 dc 24 21 53 8d b7 c3 8a 12 83 97 0e b3 1f 21 a4 7d 2e 3c c6 23 d2 9e f0 46 12 79 c7 ac f9 3b 03 1b e2 b6 9c e4 5c 93 39 55 49 57 f2 9e f6 09 f0 19 ee c9 75 98 3a 03 b5 37 62 2d 7e 0f 19 6b e1 48 f1 c7 cb b8 8e 60 2a
您的问题是在使用之前需要将密钥和IV字符串去除为字节。
答案 1 :(得分:1)
首先,我要感谢向我发送一条建议的所有人,
现在,我与您分享我的问题的解决方案,
我希望它对任何人都有帮助,
<?php
function hex2bin($hex_string) {
return pack('H*', $hex_string);
}
$data_to_encrypt = '2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~';
$key = 'abcdef0123456789abcdef0123456789';
$iv = '0000000000000000';
$key = hex2bin($key);
$iv = hex2bin($iv);
$data_encrypted = bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data_to_encrypt, MCRYPT_MODE_CBC, $iv));
echo "Data encrypted: ".strtoupper($data_encrypted)."<br/>";
echo "Length: ".strlen($data_encrypted)."<br/>";
?>
加密数据:0D5835AFEBEE04C6DC2421538DB7C38A1283970EB31F21A47D2E3CC623D29EF0461279C7ACF93B031BE2B69CE45C9339554957F29EF609F019EEC975983A03B537622D7E0F196BE148F1C7CBB88E602A
长度:160
上帝保佑你