您好,登录后才能下订单哦!
本篇内容介绍了“如何解决Ruby+OpenSSL 3DES加密后多了8字节的问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
明文“abcdefgHabcdefgH” 用key:”abcdefgH12345678“ 加密后的密文应该是 "ec3f23575fea50b2ec3f23575fea50b2。但使用Ruby+openssl的结果是"ec3f23575fea50b2ec3f23575fea50b2 9eda321f26b6909f ", 多出了后面的8字节: 9eda321f26b6909f
# abcdefgHabcdefgH => ["ec3f23575fea50b2ec3f23575fea50b29eda321f26b6909f"](7D8jV1/qULLsPyNXX+pQsp7aMh8mtpCf) key:`abcdefgH12345678` OK # abcdefgHabcdefgH => ["ec3f23575fea50b2ec3f23575fea50b2"](7D8jV1/qULLsPyNXX+pQsg==) key:`abcdefgH12345678` OK
填充方法的使用问题
明文的长度必须是8的整数倍,否则会报错::in `final': data not multiple of block length (OpenSSL::Cipher::CipherError)
require 'openssl' module Des3 # puts OpenSSL::Cipher.ciphers def self.cipher(mode, key , data) # cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').send(mode) # cipher = OpenSSL::Cipher.new('DES-EDE3').send(mode) # 192 bits KEY cipher = OpenSSL::Cipher.new('DES-EDE').send(mode) # 128 bits key # cipher = OpenSSL::Cipher.new('DES3').send(mode) # 192 bits KEY cipher.key = key cipher.padding =0 #!!! 0:加密后不会多出8bytes # key:`abcdefgH12345678`, `abcdefgHabcdefgH`` => `["ec3f23575fea50b2ec3f23575fea50b2"]`(7D8jV1/qULLsPyNXX+pQsg==) OK # key:`abcdefgH12345678`, `abcdefgHabcdefgH`` => `["ec3f23575fea50b2ec3f23575fea50b29eda321f26b6909f"]`(7D8jV1/qULLsPyNXX+pQsp7aMh8mtpCf) OK encrypted = '' encrypted << cipher.update(data) encrypted << cipher.final encrypted end def self.encrypt(key, data) cipher(:encrypt, key, data) end def self.decrypt(key, data) cipher(:decrypt, key, data) end end def test_case(dat) key, data, dataExpected = dat[:key], dat[:data], dat[:dataExpected].downcase.gsub(" ","") dataEnrypted = Des3::encrypt(key ,data) dataDecrypted = Des3::decrypt(key ,dataEnrypted) print("key:`#{key}` `#{data}` => 0x:#{dataEnrypted.unpack("H*")[0]} #{dataDecrypted==data ? 'OK' : "FAIL"} #{dataExpected==dataEnrypted.unpack("H*")[0] ? 'OK' : "FAIL(Expected:#{dataExpected})"} \n") puts " key0x:#{key.unpack("H*")[0]}, 0x:#{data.unpack("H*")[0]} => 0x:#{dataEnrypted.unpack("H*")[0]} #{dataDecrypted==data ? 'OK' : 'FAIL'} #{dataExpected==dataEnrypted.unpack("H*")[0] ? 'OK' : 'FAIL'} " end def unit_test dat1=[ {key:"abcdefgH12345678", data:"abcdefgHabcdefgH", dataExpected:"ec3f23575fea50b2ec3f23575fea50b2"}, {key:"\x12\x34\x56\x78\x90\x12\x34\x56\xab\xcd\xef\x12\x34\x56\x78\x90",data:"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",dataExpected:"c4 fe a9 c5 6c 89 64 11 c4 fe a9 c5 6c 89 64 11"}, ] dat1.each do |dat| test_case(dat) end end unit_test
key:`abcdefgH12345678` `abcdefgHabcdefgH` => 0x:ec3f23575fea50b2ec3f23575fea50b2 OK OK key0x:61626364656667483132333435363738, 0x:61626364656667486162636465666748 => 0x:ec3f23575fea50b2ec3f23575fea50b2 OK OK key:`4Vx�4V���4Vx�` ` ` => 0x:c4fea9c56c896411c4fea9c56c896411 OK OK key0x:1234567890123456abcdef1234567890, 0x:00000000000000000000000000000000 => 0x:c4fea9c56c896411c4fea9c56c896411 OK OK
“如何解决Ruby+OpenSSL 3DES加密后多了8字节的问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。