I am trying to convert some Python code into Ruby. Here is a snippet of the Python code:
# -*- coding: UTF-8 -*-
des3Key = '1234567887654321'
iv = des3Key[0:8]
data = r'abc'
k = pyDes.triple_des(des3Key,pyDes.CBC,iv,pad=None,padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data)
data = 'abc'
key = '1234567887654321'
iv = key[0..7]
# cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3')
cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3-CBC') # or any other algorithm?
output = cipher.update(data)
output << cipher.final
p Base64.encode64(output).gsub(/\n/, "")
There are two issues here. First, don’t use
pkcs5_keyivgen, this is an old and deprecated function for deriving a key nad iv from a password. You need to set these directly on the
cipher.key = key cipher.iv = iv
Second, in your Python code the key is 16 bytes, which means you are using two key (or keying option 2) triple DES. The Python code automatically determines which to use depending on the length of the key used. In the Ruby OpenSSL bindings you need to explicitly specify which to use.
des-ede3-cbc is three key (or keying option 1). You need to use
des-ede-cbc as the cipher name:
cipher = OpenSSL::Cipher.new('des-ede-cbc')