红宝石鱼在最后的位数差异 [英] Ruby blowfish difference in the last digits

查看:155
本文介绍了红宝石鱼在最后的位数差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些来自API提供商的测试数据键/文本/加密,现在尝试使用以下功能产生相同的加密结果,但是我的结果与241位数字中最后16位中提供的结果相同。你有什么想法,原因可能是什么?
我确保,bf-ecb是正确的模式,并尝试使用url编码,但迄今没有成功。

 code> require'openssl'

def加密(key,data)
cipher = OpenSSL :: Cipher :: Cipher.new('bf-ecb')send(加密)
cipher.key = key
result = cipher.update(data)<< cipher.final

hexed =''
result.each_byte {| c | hexed< '%02x'%c}
hexed.upcase
end

更新



还尝试解密示例结果导致OpenSSL :: Cipher :: CipherErrorbad decrypt

解决方案

确实是填充问题。我解决了它,并将其自动执行。
到目前为止,它有效。



这是它的样子:

 code> require'openssl'

def加密(key,data)
cipher = OpenSSL :: Cipher :: Cipher.newbf-ecb
cipher。 padding = 0
cipher.key = key
cipher.encrypt
enhex(cipher.update padd data)
end
def decrypt(key,data,len)
cipher = OpenSSL :: Cipher :: Cipher.newbf-ecb
cipher.padding = 0
cipher.key = key
cipher.decrypt
.update dehex(data))。slice(0,len)
end
def enhex(data)
hexed =''
data.each_byte {| c | hexed< '%02x'%c}
hexed.upcase
end
def dehex(data)
data.scan(/../)。map {| b | b.to_i(16)} .pack('C *')
end
def padd(data)
data +*(8 - (data.length%8))
end


I have some testdata key/text/encrypted from an API provider and am now trying to yield the same encrypted result with the function below, but my result diverts from the provided one in the last 16 of 241 digits. Do you have an idea, what the reason may be? I ensured, that 'bf-ecb' is the right mode, and experimented with url-encoding, but so far without success.

require 'openssl'

def encrypt(key, data)
    cipher = OpenSSL::Cipher::Cipher.new('bf-ecb').send(:encrypt)
    cipher.key = key
    result = cipher.update(data) << cipher.final

    hexed = ''
    result.each_byte { |c| hexed << '%02x' % c }
    hexed.upcase
end

UPDATE

Also trying to decrypt the example result results in an OpenSSL::Cipher::CipherError "bad decrypt"

解决方案

It was indeed a problem with the padding. I worked around it with deactivating it and implementing it by myself. So far it works.

This is how it looks like:

require 'openssl'

def encrypt(key,data)
  cipher = OpenSSL::Cipher::Cipher.new "bf-ecb"
  cipher.padding = 0
  cipher.key = key
  cipher.encrypt
  enhex(cipher.update padd data)
end
def decrypt(key,data,len)
  cipher = OpenSSL::Cipher::Cipher.new "bf-ecb"
  cipher.padding = 0
  cipher.key = key
  cipher.decrypt
  (cipher.update dehex(data)).slice(0,len)
end
def enhex(data)
  hexed = ''
  data.each_byte { |c| hexed << '%02x' % c }
  hexed.upcase
end
def dehex(data)
  data.scan(/../).map{ |b| b.to_i(16) }.pack('C*')
end 
def padd(data)
  data + " "*(8 - (data.length % 8))
end

这篇关于红宝石鱼在最后的位数差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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