Ruby OpenSSL AES-128-CTR

时间:2015-05-01 14:34:28

标签: ruby openssl cryptography aes

我无法弄清楚我在尝试使用ruby的OpenSSL密码AES-128-CTR使用给定密钥解密一串十六进制值时出错了。

我正在使用gem hex_string将我的十六进制转换为字节

ctrkey = "36f18357be4dbd77f050515c73fcf9f2"
ciphertext3 = "69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc3\
88d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329"

cipher2 = OpenSSL::Cipher.new('AES-128-CTR')
cipher2.decrypt

ctrkey = ctrkey.to_byte_string
cipher2.key = ctrkey

iv = cipher2.random_iv
cipher2.iv = iv

ciphertext3 = ciphertext3.to_byte_string
plain = cipher2.update(ciphertext3) + cipher2.final

puts "plaintext of Q3: #{plain}"

我知道我错过了一些小东西,因为我有类似的代码实现AES-128-CBC。我是否需要一个计数器为密文中的每个128字节块增加IV?

0 个答案:

没有答案