So I'm using the code from this SO answer to create a 2-way alphanumeric encryption function.
Basically, the functions look like this:
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:]
These functions interact w/a database object that saves the result of encrypt_password
; to decrypt, decrypt_password
is used. (Pretty self-explanatory.)
I'm getting stuck on this line:
encrypted = unhexlify(self.password_encrypted)
Somehow, Python keeps telling me that self.password_encrypted
isn't a hex digit. I have no idea what's going on b/c it's being saved to the object as a hex digit.
By the way, the string representation is: '\x[decimal numbers]'
which I also think is strange. Shouldn't it be hexadecimal digits?
Traceback follows:
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