georgemarshall / django-cryptography

Easily encrypt data in Django

Home Page:https://django-cryptography.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

encrypt() function is not encrypting my data

lrodri14 opened this issue · comments

So i started using django-cryptography to store sensible data, i am using the encrypt function and passing a models.Charfield argument as the field to encrypt, every thing works fine, no errors, no warning, but when i check my data in the django admin page, i see the raw text, no data was encrypted, as i know i followed all the steps, i don't know if i missed any minimal step.

Python 3.7.2
Django 2.2.12
django-cryptography 1.0

from django_cryptography.fields import encrypt

class EncryptedFieldModel(models.Model)
encrypted_field = encrypt(models.Charfield(max_length=100))

I'm also having the same issue on Django 3.0.8

This particular library only encrypts data in the database. Once it has exited/retrieved from the database and is in memory and being passed around, in this case to the admin page, it will be unencrypted.

commented

True, this encrypts the data in database. You can try viewing your sample database for example sqlite3 with sqlite browser and check the fields that has the encrypt. They will show as BLOB.

Hi I am facing the same issue. Can you please elaborate on how to view sample database of sqlite3. I want to check if I am doing right.
Can someone clarify two things 1) Even though I use django-cryptography data in admin portal is not encrypted and 2) Is there any way to encrypt data in admin page

commented

@Phani7g yes. Just open your sqlite3 database in your preferred database viewing program. Go check your _ table and it should show BLOB (Binary Large Object) (the encrypted data on your enctypted model field)

Answers:

  1. This is correct, data is already decrypted while django is running. Data is encrypted on database level.
  2. There isn't, again the data is already encrypted on the database level.

You may want to take a look at other django crypto projects. Please update here if you found what you are looking for :D

@r1bnc Can you please tell me after encrypted the data in the database, how can I decrypt my data, if i want

commented

@r1bnc Can you please tell me after encrypted the data in the database, how can I decrypt my data, if i want

What do you mean decrypt data? On the database level? or within Django?
It gets decrypted once you use Django, the encryption is on the database level (data at rest). I don't know how to decrypt the data on the database itself (ex. like a database dump from mysqldump or pgdump equivalent)

commented

Decrypt means, The conversion of encrypted data into its original form. exp-I have to encrypt the user's bank account number before saving it in the database after implemented the Django cryptography package in our project, in SQLite, it shows me BLOB, now I want to decryption(Original form) the encrypted data ,

On Wed, 1 Sep 2021, 16:23 Gian, @.***> wrote: @r1bnc https://github.com/r1bnc Can you please tell me after encrypted the data in the database, how can I decrypt my data, if i want What do you mean decrypt data? On the database level? or within Django? It gets decrypted once you use Django, the encryption is on the database level (data at rest). I don't know how to decrypt the data on the database itself (ex. like a database dump from mysqldump or pgdump equivalent) — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#51 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQOJRQWZV47553VIGHLCZFLT7YA3FANCNFSM4VVYCN4A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

I understand now, someone has already posted a question similar to yours. See issue #59
Check this issue too, it could help #24
I have never tried decrypting data from the database, but you could theoretically dump the database into json to get the decrypted data, it may take some time. iirc. Afterwards, import it to a new database. Again, I am not sure if this would work.

# Dump data via manage.py
$ python3 ./manage.py dumpdata > data.json

# Load data
$ pyrhon3 ./manage.py loaddata data.json

I am using this encrypt in my project to store a particular field, It is working well incase of post and get request, but it is throwing an error in case of patch while saving. Can anybody help?