Как зашифровать и сохранить данные в базе данных, используя django и pycrpytodome? - PullRequest
2 голосов
/ 23 июня 2019

Я создал базу данных, используя django, и создал HTML-форму для хранения данных в базе данных. Теперь я хочу зашифровать с помощью pycryptodome и сохранить зашифрованный текст в базе данных. И мне нужен расшифрованный открытый текст при отображении данных из базы данных.

Я попробовал некоторые базовые примеры шифрования и алгоритмы из документации по pycrytodome. Теперь я хочу зашифровать и сохранить зашифрованный текст в базе данных.

#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")

Я хочу зашифровать открытый текст, который я ввожу в базу данных, используя форму html, и я хочу зашифровать текст, который будет сохранен в базе данных. Я хочу помочь с этим.

...