Ruby OpenSSL AES-128-CTR

我们两清 提交于 2019-12-12 06:18:37

问题


I can't figure out what I am doing wrong here trying to decrypt a string of hex values with a given key using ruby's OpenSSL cipher AES-128-CTR.

I am using the gem hex_string to convert my hex to bytes

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}"

I know I am missing something small because I have similar code implementing AES-128-CBC. Do I need to have a counter that increments the IV for each block of 128 bytes in the ciphertext?


回答1:


No, you're not missing something small, you are missing something huge.

Instead of using the same IV as used for encryption, you are generating a new one. For CTR, if the IV is random then each counter value is different, resulting in random looking output.

Often the IV (or nonce in the case of CTR) is prefixed to the ciphertext. For CTR that may be fewer bytes than 16 - although that is still the most probable size to try.



来源:https://stackoverflow.com/questions/29988869/ruby-openssl-aes-128-ctr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!