Ruby には、データの一部を復号化する次の関数があります。
def decrypt(key, iv, cipher_hex)
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.decrypt
cipher.key = key.gsub(/(..)/){|h| h.hex.chr}
cipher.iv = iv.gsub(/(..)/){|h| h.hex.chr}
decrypted_data = cipher.update(cipher_hex.gsub(/(..)/){|h| h.hex.chr})
decrypted_data << cipher.final
return decrypted_data
end
PHPでまったく同じことをしようとしていますが、何が間違っているのかわかりません。これが私が持っているものです:
function decrypt_data($key, $iv, $cipher_hex) {
return mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
hex_to_str($key),
hex_to_str($cipher_hex),
MCRYPT_MODE_CBC,
hex_to_str($iv)
);
}
function hex_to_str($hex_str) {
preg_match_all('/(..)/', $hex_str, $matches);
$to_return = '';
foreach ($matches[1] as $val)
$to_return .= chr(hexdec($val));
return $to_return;
}
出力は、探している文字列ではなく、ゴミになってしまいます。アイデア?
そして、始める前に、それをに切り替えてもMCRYPT_RIJNDAEL_256
役に立たないようで、iv がブロックサイズほど長くないという不平を言うだけです。このサイトでは、128/256 はキー サイズではなくブロック サイズを示していると述べているため、この場合は 128 が正しいと思います。