[PYTHON] Do AES encryption with DJango

Library installation

$ pip install pycryptodome $ pip install pycryptodomex

List the key in settings.py

$ vim settings.py
#AES cipher
try:
   from .aes_key import *
except ImportError:
   AES_KEY='oDZC5a6rhyukFmKCbPS6M45TFROLmrlB'
$ vim aes_key.py
AES_KEY='dMchLlLllvrDtQUxXDLpAWr2v1EdjuLU'

Verification

$ python manage.py shell
from project.settings import *
print(AES_KEY)

Creating a cipher class

import base64
import hashlib
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Util import Padding


class AESCipher(object):
   def __init__(self, key):
       self.key = (hashlib.md5(key.encode('utf-8')).hexdigest()).encode('utf-8')

   def encrypt(self, raw):
       iv = Random.get_random_bytes(AES.block_size)
       cipher = AES.new(self.key, AES.MODE_CBC, iv)
       data = Padding.pad(raw.encode('utf-8'), AES.block_size, 'pkcs7')
       return base64.b64encode(iv + cipher.encrypt(data)).decode('utf-8')

   def decrypt(self, enc):
       enc = base64.b64decode(enc.encode('utf-8'))
       iv = enc[:AES.block_size]
       cipher = AES.new(self.key, AES.MODE_CBC, iv)
       data = Padding.unpad(cipher.decrypt(enc[AES.block_size:]), AES.block_size, 'pkcs7')
       return data.decode('utf-8')

call

import random
import string
from project.lib.cipher import AESCipher

raw_text = 'Hello'

key = 'y6lLepZQpppdzjkeG5MhUaaaRCychpDd'
print('key:' + key)

cipher = AESCipher(key)

encrypted_text = cipher.encrypt(raw_text)
print(encrypted_text)

decrypted_text = cipher.decrypt(encrypted_text)
print(decrypted_text)

Recommended Posts

Do AES encryption with DJango
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Do Django with CodeStar (Python3.8, Django2.1.15)
How to do arithmetic with Django template
Internationalization with django
CRUD with Django
Django 1.11 started with Python3.6
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
About text encryption (AES encryption)
Twitter OAuth with Django
Getting Started with Django 1
Send email with Django
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
What Django renders do
Start today with Django
Getting Started with Django 2
Things to do when you start developing with Django
Get started with Django! ~ Tutorial ⑤ ~
Wai "Can Django do JOIN?"
Minimal website environment with django
Create an API with Django
Deploy Django serverless with Lambda
Python3 + Django ~ Mac ~ with Apache
Create a homepage with django
Get started with Django! ~ Tutorial ④ ~
Getting Started with Python Django (4)
Web application creation with Django
Getting Started with Python Django (3)
Combine FastAPI with Django ORM
Get started with Django! ~ Tutorial ⑥ ~
Save tweet data with Django
Let's do R-CNN with Sklearn-theano
Getting Started with Python Django (6)
Combine two images with Django
Getting Started with Django with PyCharm
Real-time web with Django Channels
Double submit suppression with Django
Nosalt AES encryption in Python
Django REST framework with Vue.js
Use prefetch_related conveniently with Django
Getting Started with Python Django (5)
Login with django rest framework
Qiita API Oauth with Django
Do something fuzzing with acceptableRegex.py
Encryption / decryption with GPG command
Do multi-stage SSH with Fabric
[Tips] How to do template extends when creating HTML with django
Test Driven Development with Django Part 3
reload in django shell with ipython
Do not switch with pyenv global!
Steps to develop Django with VSCode
Test Driven Development with Django Part 4
Load Django modules with an interpreter
Set up social login with Django
Test Driven Development with Django Part 6
Measure Django application coverage with Coverage.py