georgemarshall/django-cryptography

Decrypting fields manually from querying the db?

mangoes-git opened this issue · 1 comments

Hi, I would like to know how to decrypt fields manually from querying the database directly. I am using MySQL and the MySQL CLI.

For example, this is the content of the encrypted field:

� ap�s��vdV8\� �P5�-X�9�m��cB,j�"������> ����j�~0����zah |�_�������H��

I am using https://asecuritysite.com/encryption/ferdecode to decode it (using my secret key) but I get no output.

Thanks

Hi @mangoes-git you can try below steps to get actual value from DB

  • First fetch base64 encoded data from db
    SELECT encode(column_name, 'base64') FROM TABLE_NAME limit 1;
  • Convert base64 encoded string to python object
    `

from django.conf import settings
from django_cryptography.utils.crypto import FernetBytes
import pickle
import base64
fernet = FernetBytes(settings.CRYPTOGRAPHY_KEY)
base64_encoded_data = "...."
pickle.loads(fernet.decrypt(base64.b64decode(base64_encoded_data)))
`