How to solve the problem of adding 8 bytes after Ruby+OpenSSL 3DES encryption
This article introduces the relevant knowledge of "how to solve the problem of 8 bytes after Ruby+OpenSSL 3DES encryption". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Problem description:
The plaintext "abcdefgHabcdefgH" encrypted with key: "abcdefgH12345678" should be "ec3f23575fea50b2ec3f23575fea50b2". But the result of using Ruby+openssl is "ec3f23575fea50b2ec3f23575fea50b2 9eda321f26b6909f", with an extra 8 bytes: 9eda321f26b6909f
# abcdefgHabcdefgH = > ["ec3f23575fea50b2ec3f23575fea50b29eda321f26b6909f"] (7D8jV1/qULLsPyNXX+pQsp7aMh8mtpCf) key: `abcdefgH12345678` OK # abcdefgHabcdefgH = > ["ec3f23575fea50b2ec3f23575fea50b2"] (7D8jV1) key: `abcdefgH12345678` OK reason:
Problems in the use of filling methods
The resolved code:
The length of plaintext must be an integer multiple of 8, otherwise an error will be reported:: 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` will not be added after encryption `abcdefgHabcdefgH`` = > `["ec3f23575fea50b2ec3f23575fea50b2"]` (7D8jV1According qULLsPyNXXSecretpQsgkeeper =) OK # key: `abcdefgH12345678`, `abcdefgHabcdefgH`` = > `["ec3f23575fea50b2ec3f23575fea50b29eda321f26b6909f"]` (7D8jV1/qULLsPyNXX+pQsp7aMh8mtpCf) OK encrypted =''encrypted 0x:ec3f23575fea50b2ec3f23575fea50b2 OK OK key0x:61626364656667483132333435363738, 0x:61626364656667486162636465666748 = > 0x:ec3f23575fea50b2ec3f23575fea50b2 OK OKkey: `4Vx' 4V '4Vx defg` = > 0x:c4fea9c56c896411c4fea9c56c896411 OK OK key0x:1234567890123456abcdef1234567890 0x:00000000000000000000000000000000 = > 0x:c4fea9c56c896411c4fea9c56c896411 OK OK, "how to solve the problem of adding 8 bytes after Ruby+OpenSSL 3DES encryption" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!