红宝石鱼在最后的位数差异 [英] Ruby blowfish difference in the last digits
问题描述
我确保,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屋!