I created a database using django and created a html form to store the data into the database. Now, I want to encrypt using pycryptodome and store the ciphertext into the database. And I want the decrypted plaintext when I display the data from the database.
I have tried some basic encryption examples and algorithms from pycrytodome documentation. Now, I want to encrypt and store the ciphertext into the database.
#This is the function where I want to encrypt the data in the get_password variable and store it
def add(request):
get_name = request.POST["add_name"]
get_password = request.POST["add_password"]
print(type(get_name),type(get_password))
s = Student(student_name=get_name,student_password=get_password)
context = { "astudent":s }
s.save()
return render(request,"sms_2/add.html",context)
#This is the example I have tried from pycryptodome documentation.
from Crypto.Cipher import AES
key=b"Sixteen byte key"
cipher=AES.new(key,AES.MODE_EAX)
data=b"This is a secret@@!!"
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(data)
print(nonce)
print(key)
print(cipher)
print(ciphertext)
print(tag)
cipher1 = AES.new(key, AES.MODE_EAX, nonce=nonce)
plaintext = cipher1.decrypt(ciphertext)
try:
cipher1.verify(tag)
print("The message is authentic:", plaintext)
except ValueError:
print("Key incorrect or message corrupted")
I want to encrypt the plaintext I enter into the database using the html form and I want to ciphertext to be stored into the database. I want help with that.