Python unhexlify十六进制到alpha转换失败

时间:2016-07-05 20:48:33

标签: python hex

所以我使用this SO answer中的代码创建了一个双向字母数字加密函数。

基本上,函数看起来像这样:

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

1 个答案:

答案 0 :(得分:1)

好的,所以我对你发布的文件做了一些修改......

CustomerToTeamTemplate customerToTeamTemplate = new CustomerToTeamTemplate(this.Context);

//Relation to CustomerToClients
new ReportRelationMapping
{
    LocalRelation = this.customerMapper.GetForeignRelation(x => x.Id, this.ReportRelationsMappings),
    ForeignRelation = this.customerToTeamMapper.GetForeignRelation(x => x.CustomerId, customerToTeamTemplate.ReportRelationsMappings),
},

But this isn't working for me because I am creating an infinate loop when passing this.ReportRelationsMappings to it self.

它可能与您的环境键变量有关,请确保它符合16,24或32字节构造。对于AES初始化第一个参数。

关于'\ x {0-9a-fA-f} {0-9a-fA-f}'格式,据我所知,这是一种字符串格式的十六进制输出