From: eusou15@... Date: 2018-01-01T21:52:07+00:00 Subject: [ruby-core:84579] [Ruby trunk Bug#14265] Possible bug in OpenSSL library Issue #14265 has been reported by careta (Dumb Thomas). ---------------------------------------- Bug #14265: Possible bug in OpenSSL library https://bugs.ruby-lang.org/issues/14265 * Author: careta (Dumb Thomas) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu] (Debian Stable) * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN ---------------------------------------- Hi, I'm not sure if this is a bug or not, but there is a difference between Ruby and Python when using Blowfish encryption in ECB mode. Using the same algorithm parameters, and the same input, the encrypted output is completely different. Behold these two code samples: RUBY================== require 'digest' require 'openssl' cleartext = 'whatevs' md5 = Digest::MD5.new md5.update(cleartext) payload = (md5.digest + cleartext).ljust(0x80, "\x00") puts "plaintext: " + payload.unpack('H*')[0] secret_key = "supersecretsupersecret" cipher = OpenSSL::Cipher::Cipher.new("bf-ecb").send :encrypt cipher.key = secret_key cipher.padding = 0 binary_data = (cipher.update(payload) << cipher.final) puts "ciphertext: " + binary_data.unpack('H*')[0] cipher = OpenSSL::Cipher::Cipher.new("bf-ecb").send :decrypt cipher.key = secret_key cipher.padding = 0 str = (cipher.update(binary_data) << cipher.final) puts "decrypted: " + str.unpack('H*')[0] RUBY================== PYTHON================ from Crypto.Cipher import Blowfish from Crypto.Hash import MD5 import binascii cleartext = 'whatevs' md5_key = MD5.new(cleartext).digest() payload = (md5_key + cleartext).ljust(0x80, "\x00") print "plaintext: " + binascii.hexlify(payload) secret_key = "supersecretsupersecret" ciphertext = Blowfish.new(secret_key, Blowfish.MODE_ECB).encrypt(payload) print "ciphertext: " + binascii.hexlify(ciphertext) print "decrypted: " + binascii.hexlify(Blowfish.new(secret_key, 1).decrypt(ciphertext)) PYTHON================ It seems that the Ruby OpenSSL makes some assumptions with regards to key length - I'm not sure if this is a bug or not. The fix for the code I pasted before was very simple: when initializing the cipher, I have to specify the key_len before setting the key: cipher = OpenSSL::Cipher::Cipher.new("bf-ecb").send :encrypt cipher.key_len = secret_key.length cipher.key = secret_key cipher.padding = 0 binary_data = (cipher.update(payload) << cipher.final) This makes it work correctly and match the Python output. -- https://bugs.ruby-lang.org/ Unsubscribe: