如何在十六进制的Ruby中执行三重DES计算? [英] How to perform Triple DES calculations in Ruby in hexadecimal?

查看:196
本文介绍了如何在十六进制的Ruby中执行三重DES计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ruby中做一些三重DES加密。我正在尝试复制此页面的结果: http://da.nmilne.com/des。 html



我试图在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] 

任何关于我出错的指针?




解决方案

密钥是十六进制的 - 如果你看看粘贴的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?

解决方案

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屋!

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