memory: fixes encoding issues during encryption
This commit is contained in:
parent
e0f8d09bf2
commit
8281c77c60
1 changed files with 17 additions and 9 deletions
|
|
@ -42,17 +42,18 @@ class BlockCipher(object):
|
|||
|
||||
Based on http://stackoverflow.com/a/12525165
|
||||
|
||||
@param key (str): the encryption key
|
||||
@param text (str): the text to encrypt
|
||||
@param key (unicode): the encryption key
|
||||
@param text (unicode): the text to encrypt
|
||||
@param leave_empty (bool): if True, empty text will be returned "as is"
|
||||
@return: Deferred: base-64 encoded str
|
||||
"""
|
||||
if leave_empty and text == '':
|
||||
return succeed(text)
|
||||
iv = BlockCipher.getRandomKey()
|
||||
key = key.encode('utf-8')
|
||||
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
|
||||
cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
d = deferToThread(cipher.encrypt, BlockCipher.pad(text))
|
||||
d = deferToThread(cipher.encrypt, BlockCipher.pad(text.encode('utf-8')))
|
||||
d.addCallback(lambda ciphertext: b64encode(iv + ciphertext))
|
||||
return d
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ class BlockCipher(object):
|
|||
|
||||
Based on http://stackoverflow.com/a/12525165
|
||||
|
||||
@param key (str): the decryption key
|
||||
@param key (unicode): the decryption key
|
||||
@param ciphertext (base-64 encoded str): the text to decrypt
|
||||
@param leave_empty (bool): if True, empty ciphertext will be returned "as is"
|
||||
@return: Deferred: str or None if the password could not be decrypted
|
||||
|
|
@ -71,6 +72,7 @@ class BlockCipher(object):
|
|||
return succeed('')
|
||||
ciphertext = b64decode(ciphertext)
|
||||
iv, ciphertext = ciphertext[:BlockCipher.IV_SIZE], ciphertext[BlockCipher.IV_SIZE:]
|
||||
key = key.encode('utf-8')
|
||||
key = key[:BlockCipher.MAX_KEY_SIZE] if len(key) >= BlockCipher.MAX_KEY_SIZE else BlockCipher.pad(key)
|
||||
cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
d = deferToThread(cipher.decrypt, ciphertext)
|
||||
|
|
@ -79,7 +81,7 @@ class BlockCipher(object):
|
|||
# a decrypted empty value and a decryption failure... both return
|
||||
# the empty value. Fortunately, we detect empty passwords beforehand
|
||||
# thanks to the "leave_empty" parameter which is used by default.
|
||||
d.addCallback(lambda text: text if text else None)
|
||||
d.addCallback(lambda text: text.decode('utf-8') if text else None)
|
||||
return d
|
||||
|
||||
@classmethod
|
||||
|
|
|
|||
Loading…
Reference in a new issue