本篇內(nèi)容介紹了“如何解決Ruby+OpenSSL 3DES加密后多了8字節(jié)的問題”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
在浦口等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供做網(wǎng)站、網(wǎng)站設計 網(wǎng)站設計制作按需網(wǎng)站建設,公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,品牌網(wǎng)站設計,全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站制作,浦口網(wǎng)站建設費用合理。
明文“abcdefgHabcdefgH” 用key:”abcdefgH12345678“ 加密后的密文應該是 "ec3f23575fea50b2ec3f23575fea50b2。但使用Ruby+openssl的結(jié)果是"ec3f23575fea50b2ec3f23575fea50b2 9eda321f26b6909f ", 多出了后面的8字節(jié): 9eda321f26b6909f
# abcdefgHabcdefgH => ["ec3f23575fea50b2ec3f23575fea50b29eda321f26b6909f"](7D8jV1/qULLsPyNXX+pQsp7aMh8mtpCf) key:`abcdefgH12345678` OK # abcdefgHabcdefgH => ["ec3f23575fea50b2ec3f23575fea50b2"](7D8jV1/qULLsPyNXX+pQsg==) key:`abcdefgH12345678` OK
填充方法的使用問題
明文的長度必須是8的整數(shù)倍,否則會報錯::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字節(jié)的問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!