Ruby OpenSSL AES-128-CTR [英] Ruby OpenSSL AES-128-CTR

查看:197
本文介绍了Ruby OpenSSL AES-128-CTR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ruby的OpenSSL密码AES-128-CTR解密具有给定密钥的十六进制值字符串时,我无法弄清楚我在做什么。



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

>

<预类= 郎-RB prettyprint-越权> ctrkey = 36f18357be​​4dbd77f050515c73fcf9f2
ciphertext3 = 69dda8455c7dd4254bf353b773304eec0ec7702330098ce7f7520d1cbbb20fc3\
88d1b0adb5054dbd7370849dbf0b88d393f252e764f1f5f7ad97ef79d59ce29f5f51eeca32eabedd9afa9329

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

ctrkey = ctrkey.to_byte_string
cipher2。密钥= ctrkey

iv = cipher2.random_iv
cipher2.iv = iv

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

放置 Q3的纯文本:#{plain}



<我知道我想念有点小,因为我有实现AES-128-CBC的类似代码。我是否需要一个计数器来为密文中每个128字节的块增加IV?

解决方案

不,您是不要错过一些小东西,而是想念一些大东西。



您将生成一个新的IV,而不是使用与加密相同的IV。对于CTR,如果IV是随机的,则每个计数器的值都是不同的,从而导致输出看起来是随机的。



通常,IV(或者是CTR)为前缀。密文。对于CTR,其字节数可能少于16-尽管这仍然是尝试的最可能的大小。


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?

解决方案

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.

这篇关于Ruby OpenSSL AES-128-CTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆