From 2d50a85caa43ac35597b1ff202c125fa6d0298ee Mon Sep 17 00:00:00 2001 From: Klafyvel Date: Mon, 22 Jan 2018 21:26:37 +0100 Subject: [PATCH] Password encryption --- settings/aes_field.py | 63 +++++++++++++++++++++++++++++++++++++++++++ settings/models.py | 13 +-------- site_tps/settings.py | 2 ++ 3 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 settings/aes_field.py diff --git a/settings/aes_field.py b/settings/aes_field.py new file mode 100644 index 0000000..d48e9c4 --- /dev/null +++ b/settings/aes_field.py @@ -0,0 +1,63 @@ +import string +import binascii +from random import choice +from Crypto.Cipher import AES + +from django.db import models +from django.conf import settings + +EOD = '`%EofD%`' # This should be something that will not occur in strings + + +def genstring(length=16, chars=string.printable): + return ''.join([choice(chars) for i in range(length)]) + + +def encrypt(key, s): + obj = AES.new(key) + datalength = len(s) + len(EOD) + if datalength < 16: + saltlength = 16 - datalength + else: + saltlength = 16 - datalength % 16 + ss = ''.join([s, EOD, genstring(saltlength)]) + return obj.encrypt(ss) + + +def decrypt(key, s): + obj = AES.new(key) + ss = obj.decrypt(s) + print(ss) + return ss.split(bytes(EOD, 'utf-8'))[0] + + +class AESEncryptedField(models.CharField): + def save_form_data(self, instance, data): + setattr(instance, self.name, + binascii.b2a_base64(encrypt(settings.AES_KEY, data))) + + def to_python(self, value): + if value is None: + return None + return decrypt(settings.AES_KEY, + binascii.a2b_base64(value)).decode('utf-8') + + def from_db_value(self, value, expression, connection, *args): + print('from db') + print(value) + if value is None: + return value + return decrypt(settings.AES_KEY, + binascii.a2b_base64(value)).decode('utf-8') + + def get_prep_value(self, value): + print('get prep value') + print(value) + print(binascii.b2a_base64(encrypt( + settings.AES_KEY, + value + ))) + return binascii.b2a_base64(encrypt( + settings.AES_KEY, + value + )) diff --git a/settings/models.py b/settings/models.py index b4a4ddd..18082ca 100644 --- a/settings/models.py +++ b/settings/models.py @@ -1,17 +1,6 @@ from django.db import models -import binascii -from site_tps import qaes -from django.conf import settings - -class AESEncryptedField(models.CharField): - def save_form_data(self, instance, data): - setattr(instance, self.name, - binascii.b2a_base64(qaes.encrypt(settings.AES_KEY, data))) - - def value_from_object(self, obj): - return qaes.decrypt(settings.AES_KEY, - binascii.a2b_base64(getattr(obj, self.attname))) +from .aes_field import AESEncryptedField class ContentSettings(models.Model): diff --git a/site_tps/settings.py b/site_tps/settings.py index 187e172..0905a1b 100644 --- a/site_tps/settings.py +++ b/site_tps/settings.py @@ -22,6 +22,8 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '1o5n7!p2@@bcd9om*6^d^=@es==*yf2^^cax=j(ij4s3#9-y(m' +AES_KEY = "0123456789ABCDEF" + # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True