您好,登錄后才能下訂單哦!
本篇內容介紹了“如何解決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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。