如何在十六进制的Ruby中执行三重DES计算? [英] How to perform Triple DES calculations in Ruby in hexadecimal?
问题描述
我试图在Ruby中复制这些结果。我怀疑问题是键应该是一个字符串,但是我需要传递一个十六进制的键。或者被加密的字符串格式错误。或者也许两者。 : - )
require'openssl'
des = OpenSSL :: Cipher :: Cipher.new(des- ede-cbc)
des.encrypt
des.key =23232323232323234545454545454545
des.update(0000000000000000)
res = des.final
res。 unpack('H *')
=> [5045c5d37ca4d13b]
但应该是:
=> [3a42d7a1d1c60c40]
任何关于我出错的指针?
- 在这里记录了不同的3DES算法: http://www.openssl.org/docs/apps/enc.html
- 为了清楚起见编辑
密钥是十六进制的 - 如果你看看粘贴的Java页面,你可以很容易地通过解码详细输出中的键的二进制值。
>> des_cbc = OpenSSL :: Cipher :: Cipher.new(des-ede-cbc)
=> #< OpenSSL的::密码::密码:0x10116ce28>
>> des_cbc.encrypt
=> #< OpenSSL的::密码::密码:0x10116ce28>
>> des_cbc.key =\x23* 8< \x45* 8
=> ######## EEEEEEEE
>> des_cbc.update(\x00* 8).unpack('H *')
=> [3a42d7a1d1c60c40]
I'm trying to do some triple DES encryption in Ruby. I'm trying to replicate the results from this page: http://da.nmilne.com/des.html
I'm trying to replicate those result in Ruby. I suspect the problem is the key is supposed to be a string, but I need to pass in a Hexadecimal key. Either that or the string being encrypted is in the wrong format. Or maybe both. :-)
require 'openssl'
des = OpenSSL::Cipher::Cipher.new("des-ede-cbc")
des.encrypt
des.key="23232323232323234545454545454545"
des.update("0000000000000000")
res=des.final
res.unpack('H*')
=> ["5045c5d37ca4d13b"]
But it should be:
=> ["3a42d7a1d1c60c40"]
Any pointers on where I'm going wrong?
- Different 3DES algorithms are documented here: http://www.openssl.org/docs/apps/enc.html
- Edited for clarity
The key is in hex - if you look at the Java page you pasted you can see that easily by decoding the binary values for the key in the detailed output.
>> des_cbc=OpenSSL::Cipher::Cipher.new("des-ede-cbc")
=> #<OpenSSL::Cipher::Cipher:0x10116ce28>
>> des_cbc.encrypt
=> #<OpenSSL::Cipher::Cipher:0x10116ce28>
>> des_cbc.key="\x23"*8 << "\x45"*8
=> "########EEEEEEEE"
>> des_cbc.update("\x00"*8).unpack('H*')
=> ["3a42d7a1d1c60c40"]
这篇关于如何在十六进制的Ruby中执行三重DES计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!