From f971411bbeb67a6da24ba3912c312569ea375967 Mon Sep 17 00:00:00 2001 From: mohan3d Date: Wed, 6 Jan 2016 12:41:50 +0200 Subject: [PATCH 1/2] minor changes to work with python3 --- SimpleAES/__init__.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/SimpleAES/__init__.py b/SimpleAES/__init__.py index 9c45d8c..de347bd 100644 --- a/SimpleAES/__init__.py +++ b/SimpleAES/__init__.py @@ -3,7 +3,12 @@ import base64 import hashlib import warnings -from StringIO import StringIO + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + from Crypto.Cipher import AES from .version import VERSION from .exceptions import EncryptionError, DecryptionError @@ -32,7 +37,7 @@ def check_output(cmd, input_=None, *popenargs, **kwargs): def _random_noise(len): - return ''.join(chr(random.randint(0, 0xFF)) for i in range(len)) + return ''.join(chr(random.randint(0, 0xFF)) for i in range(len)).encode() class SimpleAES(object): @@ -41,6 +46,7 @@ def __init__(self, password): self._password = password def encrypt(self, string): + string = string.encode() """Encrypts a string using AES-256.""" try: envvar = hashlib.sha256(_random_noise(16)).hexdigest() @@ -55,8 +61,7 @@ def encrypt(self, string): def decrypt(self, b64_ciphertext, legacy='auto'): """Decrypts a string using AES-256.""" - if legacy is True or (legacy == 'auto' and - not b64_ciphertext.startswith('U2Fsd')): + if legacy is True or (legacy == 'auto' and not b64_ciphertext.startswith(b'U2Fsd')): return self._legacy_decrypt(b64_ciphertext) try: @@ -64,7 +69,7 @@ def decrypt(self, b64_ciphertext, legacy='auto'): plaintext = check_output([ 'openssl', 'enc', '-d', '-aes-256-cbc', '-a', '-pass', 'env:{0}'.format(envvar)], - input_=b64_ciphertext + '\n', + input_=b64_ciphertext + b'\n', env={envvar: self._password}) return plaintext except: @@ -128,4 +133,4 @@ def base64_decrypt(self, ciphertext): ciphertext = aes.encrypt(input) text = aes.decrypt(ciphertext) assert text == input - print 'All OK' + print('All OK') From a3fd937059ffda91c6d0ea271262c48508754e55 Mon Sep 17 00:00:00 2001 From: mohan3d Date: Wed, 6 Jan 2016 13:00:09 +0200 Subject: [PATCH 2/2] minor changes to work with python2 and python3 --- SimpleAES/__init__.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/SimpleAES/__init__.py b/SimpleAES/__init__.py index de347bd..e2532ea 100644 --- a/SimpleAES/__init__.py +++ b/SimpleAES/__init__.py @@ -12,6 +12,7 @@ from Crypto.Cipher import AES from .version import VERSION from .exceptions import EncryptionError, DecryptionError +from sys import version_info as python_version __title__ = 'SimpleAES' __version__ = VERSION @@ -19,6 +20,8 @@ __license__ = 'BSD' __copyright__ = 'Copyright 2012 Vincent Driessen' +python_3 = python_version >= (3, 0) + def check_output(cmd, input_=None, *popenargs, **kwargs): """Custom variant of stdlib's check_output(), but with stdin feed support.""" @@ -37,7 +40,8 @@ def check_output(cmd, input_=None, *popenargs, **kwargs): def _random_noise(len): - return ''.join(chr(random.randint(0, 0xFF)) for i in range(len)).encode() + s = ''.join(chr(random.randint(0, 0xFF)) for i in range(len)) + return s.encode() if python_3 else s class SimpleAES(object): @@ -46,7 +50,8 @@ def __init__(self, password): self._password = password def encrypt(self, string): - string = string.encode() + if python_3: + string = string.encode() """Encrypts a string using AES-256.""" try: envvar = hashlib.sha256(_random_noise(16)).hexdigest() @@ -61,15 +66,17 @@ def encrypt(self, string): def decrypt(self, b64_ciphertext, legacy='auto'): """Decrypts a string using AES-256.""" - if legacy is True or (legacy == 'auto' and not b64_ciphertext.startswith(b'U2Fsd')): + c = b'U2Fsd' if python_3 else 'U2Fsd' + if legacy is True or (legacy == 'auto' and not b64_ciphertext.startswith(c)): return self._legacy_decrypt(b64_ciphertext) try: envvar = hashlib.sha256(_random_noise(16)).hexdigest() + endline = b'\n' if python_3 else '\n' plaintext = check_output([ 'openssl', 'enc', '-d', '-aes-256-cbc', '-a', '-pass', 'env:{0}'.format(envvar)], - input_=b64_ciphertext + b'\n', + input_=b64_ciphertext + endline, env={envvar: self._password}) return plaintext except: @@ -134,3 +141,4 @@ def base64_decrypt(self, ciphertext): text = aes.decrypt(ciphertext) assert text == input print('All OK') +