使用现有Python代码在Ruby中实现三重DES
作为从python到ruby转换的一部分,我们需要将现有的python代码重新写入ruby。python中的加密代码如下所示使用现有Python代码在Ruby中实现三重DES,python,ruby,encryption,cryptography,tripledes,Python,Ruby,Encryption,Cryptography,Tripledes,作为从python到ruby转换的一部分,我们需要将现有的python代码重新写入ruby。python中的加密代码如下所示 import md5; from pyDes import * PrivateKey = 'secret'; XmlToEncrypt = 'Hello'; #Generate an MD5Hash on the Private key m = md5.new(PrivateKey); #create a 16 byte string key = m.digest(
import md5;
from pyDes import *
PrivateKey = 'secret';
XmlToEncrypt = 'Hello';
#Generate an MD5Hash on the Private key
m = md5.new(PrivateKey);
#create a 16 byte string
key = m.digest();
#setup the Triple DES class from pyDes
payloadEncrypt = triple_des(key, ECB, '\0\0\0\0\0\0\0\0', pad=None, padmode=PAD_PKCS5);
#Encrypt the xml Payload
EncryptedXML = payloadEncrypt.encrypt(XmlToEncrypt).encode('base64');
#Finally convert to base64, this string will be used in creating the HMAC signature
EncryptedXMLString = EncryptedXML.encode('base64');
print "Encrypted: " + EncryptedXML;
def triple_des_xml_payload(data)
# Get xml payload data and apply triple-des algorithm on it and convert it to base64
md5 = Digest::MD5.hexdigest(@private_key) # Create a md5 Hash
cipher = OpenSSL::Cipher::Cipher.new('DES-ECB')
cipher.encrypt
cipher.pkcs5_keyivgen(md5)
output = cipher.update(data)
output << cipher.final
encrypted_xml = Base64.encode64(output)
puts encrypted_xml
return encrypted_xml
end
我们试图实现如下相同的功能
import md5;
from pyDes import *
PrivateKey = 'secret';
XmlToEncrypt = 'Hello';
#Generate an MD5Hash on the Private key
m = md5.new(PrivateKey);
#create a 16 byte string
key = m.digest();
#setup the Triple DES class from pyDes
payloadEncrypt = triple_des(key, ECB, '\0\0\0\0\0\0\0\0', pad=None, padmode=PAD_PKCS5);
#Encrypt the xml Payload
EncryptedXML = payloadEncrypt.encrypt(XmlToEncrypt).encode('base64');
#Finally convert to base64, this string will be used in creating the HMAC signature
EncryptedXMLString = EncryptedXML.encode('base64');
print "Encrypted: " + EncryptedXML;
def triple_des_xml_payload(data)
# Get xml payload data and apply triple-des algorithm on it and convert it to base64
md5 = Digest::MD5.hexdigest(@private_key) # Create a md5 Hash
cipher = OpenSSL::Cipher::Cipher.new('DES-ECB')
cipher.encrypt
cipher.pkcs5_keyivgen(md5)
output = cipher.update(data)
output << cipher.final
encrypted_xml = Base64.encode64(output)
puts encrypted_xml
return encrypted_xml
end
def triple_des_xml_有效负载(数据)
#获取xml有效负载数据并对其应用三重des算法,并将其转换为base64
md5=摘要::md5.hexdigest(@private_key)#创建一个md5哈希
cipher=OpenSSL::cipher::cipher.new('DES-ECB'))
加密
密码pkcs5_keyivgen(md5)
输出=cipher.update(数据)
输出您的Python代码使用2-key(或“Keying option 2”)三重DES(因为您使用MD5哈希的128位结果作为密钥)。要在Ruby中获得相同的密码,需要将密码指定为des ede ecb
在Python代码中,您使用机密的MD5散列作为密钥。因此,在Ruby中,您不需要pkcs5\u keyivgen
,只需直接使用此哈希结果作为密钥。还要确保使用实际的哈希摘要,而不是十六进制表示
结合这两个问题:
def triple_des_xml_payload(data)
# Note: digest not hexdigest
key = Digest::MD5.digest(@private_key) # Create a md5 Hash
# Just use OpenSSL::Cipher, not OpenSSL::Cipher::Cipher
cipher = OpenSSL::Cipher.new('des-ede-ecb')
cipher.encrypt
# Specify the key (no need for pkcs5_keyivgen)
cipher.key = key
output = cipher.update(data)
output << cipher.final
encrypted_xml = Base64.encode64(output)
return encrypted_xml
end
def triple_des_xml_有效负载(数据)
#注意:摘要不是摘要
key=Digest::MD5.Digest(@private_key)#创建一个MD5哈希
#只需使用OpenSSL::Cipher,而不是OpenSSL::Cipher::Cipher
cipher=OpenSSL::cipher.new('des-ede-ecb')
加密
#指定密钥(不需要pkcs5\U keyivgen)
cipher.key=密钥
输出=cipher.update(数据)
输出您的Python代码使用2-key(或“Keying option 2”)三重DES(因为您使用MD5哈希的128位结果作为密钥)。要在Ruby中获得相同的密码,需要将密码指定为des ede ecb
在Python代码中,您使用机密的MD5散列作为密钥。因此,在Ruby中,您不需要pkcs5\u keyivgen
,只需直接使用此哈希结果作为密钥。还要确保使用实际的哈希摘要,而不是十六进制表示
结合这两个问题:
def triple_des_xml_payload(data)
# Note: digest not hexdigest
key = Digest::MD5.digest(@private_key) # Create a md5 Hash
# Just use OpenSSL::Cipher, not OpenSSL::Cipher::Cipher
cipher = OpenSSL::Cipher.new('des-ede-ecb')
cipher.encrypt
# Specify the key (no need for pkcs5_keyivgen)
cipher.key = key
output = cipher.update(data)
output << cipher.final
encrypted_xml = Base64.encode64(output)
return encrypted_xml
end
def triple_des_xml_有效负载(数据)
#注意:摘要不是摘要
key=Digest::MD5.Digest(@private_key)#创建一个MD5哈希
#只需使用OpenSSL::Cipher,而不是OpenSSL::Cipher::Cipher
cipher=OpenSSL::cipher.new('des-ede-ecb')
加密
#指定密钥(不需要pkcs5\U keyivgen)
cipher.key=密钥
输出=cipher.update(数据)
输出如果我没有弄错的话,Ruby OpenSSL包装器使用3DES
来命名triple-DES。操作模式似乎相同,但请确保使用相同的密钥大小。您可以在密码的名称中指定它。另外,建议使用,而不是。我们已经更改了PKCS5,但仍然是def get_key(pass)salt=OpenSSL::Random.Random_bytes(32)iter=20000 key_len=32 key=OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass,salt,iter,key_len)end,但仍然没有luckIf我没弄错,RubyOpenSSL包装器使用3DES
来命名三重DES。操作模式似乎相同,但请确保使用相同的密钥大小。您可以在密码的名称中指定它。另外,建议使用,而不是。我们已经更改了PKCS5,但仍然是def get_key(pass)salt=OpenSSL::Random.Random_bytes(32)iter=20000 key_len=32 key=OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass,salt,iter,key_len)结束,但仍然没有运气