# Implementing encryption for MySQL fields' data in Django

**URL:** <https://forum.djangoproject.com/t/implementing-encryption-for-mysql-fields-data-in-django/30409>\
**Category:** Using the ORM\
**Created:** [April 22, 2024, 11:18am UTC](https://forum.djangoproject.com/t/implementing-encryption-for-mysql-fields-data-in-django/30409 "2024-04-22T11:18:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [April 22, 2024, 11:18am UTC](https://forum.djangoproject.com/t/implementing-encryption-for-mysql-fields-data-in-django/30409/1 "2024-04-22T11:18:40Z")

</div>

I have an existing database in LAMP which I need to import to our Django application.  
There are some 5 columns which I need the values encrypted.  
I am looking at [this article](https://www.linkedin.com/pulse/encrypting-confidential-information-django-anil-kumar-valluru/) for reference.

1. Can I use Django’s SECRET\_KEY in settings.py as the key for encrypt/decrypt ?

2. a. Is there an alternative quicker way to store and retrieve encrypted data in MySQL where MySQL handles this automatically ?

3. b. Can Django’s ORM handle this by us passing the SECRET\_KEY to the ORM functions ?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 22, 2024, 1:13pm UTC](https://forum.djangoproject.com/t/implementing-encryption-for-mysql-fields-data-in-django/30409/2 "2024-04-22T13:13:09Z")

</div>

First, as a general note, you may need to consider the Legal / Regulatory implications of what you’re doing here. If you’re just doing this to satisfy some checkbox on an internal audit, that’s one thing. But if there’s some regulatory requirement that you’re trying to fulfill, I **strongly** suggest that you do **not** rely upon any information you receive in this forum without verifying it with a Security Architect that understands those regulations and the requirements that go with them. (Saying that you got your solution on-line - regardless of the source - will _not_ protect you from fines or lawsuits.)

> [@anjanesh](#):
>
> Can I use Django’s SECRET\_KEY in settings.py as the key for encrypt/decrypt ?

You really don’t want to do that. There could be situations where one needs to be changed without affecting the other.

However, it could be viable to create a new setting for it if you wish to do it that way. (How that setting gets defined or implemented is a different issue, see the note above.)

> [@anjanesh](#):
>
> - Is there an alternative quicker way to store and retrieve encrypted data in MySQL where MySQL handles this automatically ?

Not “automatically”. You’ll still have the whole key-management issue. But [MySQL provides database functions](https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html) to support this.

> [@anjanesh](#):
>
> Can Django’s ORM handle this by us passing the SECRET\_KEY to the ORM functions ?

Not intrinsically. I’ve found a couple of PostgreSQL-related libraries that support encrypted fields. You could look at them to see how much work it’ll be to convert one of them to MySQL.

Finally, I’m aware of two quotes that I would love to attribute appropriately, but I can’t find definitive sources for either:

- If you think technology can solve your security problems, then you don’t understand the problems and you don’t understand the technology.

- Cryptography is not magic pixie dust that you can sprinkle on a system to make it secure

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [April 23, 2024, 3:31am UTC](https://forum.djangoproject.com/t/implementing-encryption-for-mysql-fields-data-in-django/30409/3 "2024-04-23T03:31:25Z")

</div>

Thank you for your detailed response Ken - this is for compliance - the data we need encrypted are access logins / keys of users to various 3rd party-systems.

We’re now exploring Azure Key Vault for this purpose (with key rotation).  
The other option that was under consideration was open-source version of hashicorp vault.
