我试图在Flask0.12.2,Python3.6.1中集成第三方支付网关(CCAvenue)。
第三方提供的参考代码使用不推荐的库md5加密文本。
我已经找到了在Existing Solution in Django中迁移的解决方案。但是,我需要相同的版本代码。
发布于 2018-12-15 07:24:50
我找到了解决方案,这是代码
from Crypto.Cipher import AES
import hashlib
from binascii import hexlify, unhexlify
def pad(data):
length = 16 - (len(data) % 16)
data += chr(length)*length
return data
def unpad(data):
return data[0:-ord(data[-1])]
def encrypt(plainText, workingKey):
iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
plainText = pad(plainText)
bytearrayWorkingKey = bytearray()
bytearrayWorkingKey.extend(map(ord, workingKey))
enc_cipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
return hexlify(enc_cipher.encrypt(plainText)).decode('utf-8')
def decrypt(cipherText, workingKey):
iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
encryptedText = unhexlify(cipherText)
bytearrayWorkingKey = bytearray()
bytearrayWorkingKey.extend(map(ord, workingKey))
decCipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
return unpad(decCipher.decrypt(encryptedText).decode('utf-8'))发布于 2021-12-23 08:56:26
为@prash的解决方案添加一个对我有用的更新。iv was of type str
from Crypto.Cipher import AES
import hashlib
from binascii import hexlify, unhexlify
def pad(data):
length = 16 - (len(data) % 16)
data += chr(length)*length
return data
def unpad(data):
return data[0:-ord(data[-1])]
def encrypt(plainText, workingKey):
iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'.encode("utf-8")
plainText = pad(plainText)
bytearrayWorkingKey = bytearray()
bytearrayWorkingKey.extend(map(ord, workingKey))
enc_cipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
return hexlify(enc_cipher.encrypt(plainText.encode("utf-8"))).decode('utf-8')
def decrypt(cipherText, workingKey):
iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
encryptedText = unhexlify(cipherText)
bytearrayWorkingKey = bytearray()
bytearrayWorkingKey.extend(map(ord, workingKey))
decCipher = AES.new(hashlib.md5(bytearrayWorkingKey).digest(), AES.MODE_CBC, iv)
return unpad(decCipher.decrypt(encryptedText).decode('utf-8'))https://stackoverflow.com/questions/53774164
复制相似问题