因此,我正在使用this SO answer中的代码创建2向字母数字加密功能。
基本上,这些函数如下所示:
from Crypto import Random
from Crypto.Cipher import AES
from binascii import hexlify
from binascii import unhexlify
def encrypt_password(self, password):
key = current_app.config['VAULT_KEY']
iv = Random.new().read(AES.block_size)
cipher = AES.new(key.strip("\'"), AES.MODE_CFB, iv)
self.password_encrypted = hexlify(iv + cipher.encrypt(password))
def decrypt_password(self):
key = current_app.config['VAULT_KEY']
encrypted = unhexlify(self.password_encrypted)
cipher = AES.new(key.strip("\'"), AES.MODE_CFB, encrypted[:AES.block_size])
return cipher.decrypt(encrypted)[AES.block_size:]
这些函数与数据库对象进行交互,该数据库对象保存
encrypt_password
的结果;要解密,请使用decrypt_password
。 (不言自明。)我陷入了这一行:
encrypted = unhexlify(self.password_encrypted)
Python总是以某种方式告诉我
self.password_encrypted
不是十六进制数字。我不知道b / c发生了什么,它以十六进制数字形式保存到对象中。顺便说一下,字符串表示形式是:
'\x[decimal numbers]'
我也觉得很奇怪。不应该是十六进制数字吗?追溯如下:
Traceback (most recent call last):
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/<>/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/<>/venv/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/<>/venv/lib/python3.5/site-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/<>/app/public/views.py", line 64, in test_queue
return u.decrypt_password()
File "/<>/app/models.py", line 80, in decrypt_password
encrypted = unhexlify(self.password_encrypted)
binascii.Error: Non-hexadecimal digit found
最佳答案
好的,我对您发布的文件做了一些修改...
from Crypto import Random
from Crypto.Cipher import AES
from binascii import hexlify, unhexlify
import hashlib
from sys import stdout as puts
class AESCipher:
def __init__(self, key):
self.key = key
def encrypt_password(self, password):
iv = Random.new().read(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CFB, iv)
self.password_encrypted = hexlify(iv + cipher.encrypt(password))
return self.password_encrypted
def decrypt_password(self):
decrypted = unhexlify(self.password_encrypted)
cipher = AES.new(self.key, AES.MODE_CFB, decrypted[:AES.block_size])
return cipher.decrypt(decrypted)[AES.block_size:]
#############################################################################
"""sample purposes only"""
"""Here's an example, you don't have to use stdout or the block size for encyrption
I decided to use the regular base 16 block size and for 'puts' I came from a
ruby background"""
retrive_password = AESCipher(hashlib.sha256("mylittlepony").digest())
puts.write(("\n"*3) + retrieve_password.encrypt_password("agenericpassword") + ("\n" *3))
puts.flush()
puts.write(retrive_password.decrypt_password() + ("\n" *3))
puts.flush(()
它可能与您的环境键变量有关,请确保它符合16、24或32字节的结构。用于AES初始化的第一个参数。
至于'\ x {0-9a-fA-f} {0-9a-fA-f}'格式,据我了解,这是字符串格式的十六进制输出
关于python - python unhexlify十六进制到alpha转换失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38212451/