如何将 DES ECB 加密从 Python 移植到 Ruby
How to port DES ECB encryption from Python to Ruby
我正在尝试将加密函数从 python 重写为 ruby,但没有得到相同的结果。我知道 des ecb 是不安全的,不推荐使用,但是为了从 python 和 ruby.
移植的目的需要它
在 python 中使用 pyDes
,我有以下内容:
import pyDes
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = pyDes.triple_des(salt, pyDes.ECB, pad=None, padmode=pyDes.PAD_PKCS5)
encrypted = cipher.encrypt(data)
base64.b64encode(encrypted)
'b4SlfbPj6BzFJ2djzu/DTbtmeZ6erKP8'
现在我想得到与ruby相同的密文:
require "base64"
require "openssl"
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = OpenSSL::Cipher::Cipher.new('DES-ECB')
cipher.encrypt
cipher.padding=0
cipher.key = salt
encrypted = cipher.update(data)
encrypted_final = encrypted + cipher.final
Base64.encode64(encrypted_final)
"pfHDx7yTWZ4vh8+AqiklPrNb+VHhcCyA\n"
在发布问题时花了一些时间尝试一些变体后,我找到了解决方案。如果对任何人有帮助,请在此处发布以供参考 - 密码应该是 DES-EDE3
并且填充设置为 1,换行符 trim.
require "base64"
require "openssl"
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3')
cipher.encrypt
cipher.padding=0
cipher.key = salt
encrypted = cipher.update(data)
encrypted_final = encrypted + cipher.final
Base64.encode64(encrypted_final).gsub(/\n/, "")
'b4SlfbPj6BzFJ2djzu/DTbtmeZ6erKP8'
我正在尝试将加密函数从 python 重写为 ruby,但没有得到相同的结果。我知道 des ecb 是不安全的,不推荐使用,但是为了从 python 和 ruby.
移植的目的需要它在 python 中使用 pyDes
,我有以下内容:
import pyDes
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = pyDes.triple_des(salt, pyDes.ECB, pad=None, padmode=pyDes.PAD_PKCS5)
encrypted = cipher.encrypt(data)
base64.b64encode(encrypted)
'b4SlfbPj6BzFJ2djzu/DTbtmeZ6erKP8'
现在我想得到与ruby相同的密文:
require "base64"
require "openssl"
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = OpenSSL::Cipher::Cipher.new('DES-ECB')
cipher.encrypt
cipher.padding=0
cipher.key = salt
encrypted = cipher.update(data)
encrypted_final = encrypted + cipher.final
Base64.encode64(encrypted_final)
"pfHDx7yTWZ4vh8+AqiklPrNb+VHhcCyA\n"
在发布问题时花了一些时间尝试一些变体后,我找到了解决方案。如果对任何人有帮助,请在此处发布以供参考 - 密码应该是 DES-EDE3
并且填充设置为 1,换行符 trim.
require "base64"
require "openssl"
salt = 'HeresATwentyFourDigtSalt'
data = 'thing to encrypt'
cipher = OpenSSL::Cipher::Cipher.new('DES-EDE3')
cipher.encrypt
cipher.padding=0
cipher.key = salt
encrypted = cipher.update(data)
encrypted_final = encrypted + cipher.final
Base64.encode64(encrypted_final).gsub(/\n/, "")
'b4SlfbPj6BzFJ2djzu/DTbtmeZ6erKP8'