python3: adapt crypto
This commit is contained in:
parent
578492612b
commit
e55aef1371
|
@ -20,6 +20,9 @@ from Crypto.Cipher import AES
|
|||
from Crypto.Protocol.KDF import PBKDF2
|
||||
from Crypto import Random
|
||||
|
||||
from django.utils import six
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
|
||||
class DecryptionError(Exception):
|
||||
pass
|
||||
|
@ -33,7 +36,7 @@ def aes_hex_encrypt(key, data):
|
|||
aes_key = PBKDF2(key, iv)
|
||||
aes = AES.new(aes_key, AES.MODE_CFB, iv)
|
||||
crypted = aes.encrypt(data)
|
||||
return '%s%s' % (binascii.hexlify(iv[:2]), binascii.hexlify(crypted))
|
||||
return force_text(b'%s%s' % (binascii.hexlify(iv[:2]), binascii.hexlify(crypted)))
|
||||
|
||||
def aes_hex_decrypt(key, payload, raise_on_error=True):
|
||||
'''Decrypt data encrypted with aes_base64_encrypt'''
|
||||
|
@ -46,10 +49,10 @@ def aes_hex_decrypt(key, payload, raise_on_error=True):
|
|||
try:
|
||||
iv = binascii.unhexlify(iv) * 8
|
||||
crypted = binascii.unhexlify(crypted)
|
||||
except TypeError:
|
||||
except (TypeError, binascii.Error):
|
||||
if raise_on_error:
|
||||
raise DecryptionError('incorrect hexadecimal encoding')
|
||||
return None
|
||||
aes_key = PBKDF2(key, iv)
|
||||
aes = AES.new(aes_key, AES.MODE_CFB, iv)
|
||||
return aes.decrypt(crypted)
|
||||
return force_text(aes.decrypt(crypted), 'utf-8')
|
||||
|
|
Loading…
Reference in New Issue