从Python 2 md5库正确迁移到Python 3 hashlib [英] Correctly migrate from Python 2 md5 library to Python 3 hashlib

查看:325
本文介绍了从Python 2 md5库正确迁移到Python 3 hashlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django 1.11,Python 3.5.2中集成第三方支付网关( CCAvenue

I'm trying to integrate a 3rd party payment gateway (CCAvenue) in Django 1.11, Python 3.5.2

第三方提供的参考代码使用了已弃用的库md5来加密文本。

The reference code provided by the 3rd party uses the deprecated library md5 to encrypt texts.

from Crypto.Cipher import AES
import md5

def pad(data):
    length = 16 - (len(data) % 16)
    data += chr(length)*length
    return data

def encrypt(plainText,workingKey):
    iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
    plainText = pad(plainText)
    encDigest = md5.new ()
    encDigest.update(workingKey)
    enc_cipher = AES.new(encDigest.digest(), AES.MODE_CBC, iv)
    encryptedText = enc_cipher.encrypt(plainText).encode('hex')
    return encryptedText

如何使用 hashlib使上面的 encrypt()方法Python 3兼容/ code> Python 3库?您可以发布整个方法吗?

How do I make the above encrypt() method Python 3 compatible using the hashlib library of Python 3? Can you post the whole method?

推荐答案

这是Python 3兼容的方法:

Here's a Python 3 compatible way of doing the same:

from binascii import hexlify, unhexlify
from Crypto.Cipher import AES
from hashlib import md5

from django.utils.encoding import force_bytes

iv = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'

BS = 16


def _pad(data):
    return data + (BS - len(data) % BS) * chr(BS - len(data) % BS)


def _unpad(data):
    return data[0:-ord(data[-1])]


def encrypt(plain_text, working_key):
    plain_text = _pad(plain_text)
    enc_cipher = AES.new(md5(force_bytes(working_key)).digest(), AES.MODE_CBC, _iv)
    return hexlify(enc_cipher.encrypt(plain_text)).decode('utf-8')


def decrypt(cipher_text, working_key):
    encrypted_text = unhexlify(cipher_text)
    dec_cipher = AES.new(md5(force_bytes(working_key)).digest(), AES.MODE_CBC, _iv)
    return _unpad(dec_cipher.decrypt(encrypted_text).decode('utf-8'))

这篇关于从Python 2 md5库正确迁移到Python 3 hashlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆