我想在 ruby 中使用 rijndael aes128 进行加密。我有这个代码:
cipher = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
cipher.encrypt
cipher.key = 'abcdef0123456789abcdef0123456789'
cipher.iv = '0000000000000000'
encrypted = cipher.update('2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~')
encrypted << cipher.final
这是行不通的。但是使用这个 PHP 函数:
<?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
如何让我的 Ruby 代码产生相同的输出?