Skip to main content

Django Encrypted Files

Encrypt files uploaded to your Django application.

This package uses AES in CTR mode to encrypt files via an upload handler.

The upload handler encrypts data as it is recieved during upload, so only encrypted data is ever written to temporary files.

Files can then be decrypted with the included EncryptedFile class, which is a file-like object that decrypts data transparently.

Installation

Via pip:

pip install django-encrypted-files

Usage

Add the encrypted_files app to your INSTALLED_APPS setting:

settings.py

INSTALLED_APPS = [
    ...
    'encrypted_files',
    ...
]

Add an encryption key to use. This should be 16, 24, or 32 bytes long:

settings.py

AES_KEY = b'\x1a>\xf8\xcd\xe2\x8e_~V\x14\x98\xc2\x1f\xf9\xea\xf8\xd7c\xb3`!d\xd4\xe3+\xf7Q\x83\xb5~\x8f\xdd'

If you want to encrypt ALL uploaded files, add the EncryptedFileUploadHandler as the first handler:

settings.py

FILE_UPLOAD_HANDLERS = [
    "encrypted_files.uploadhandler.EncryptedFileUploadHandler",
    "django.core.files.uploadhandler.MemoryFileUploadHandler",
    "django.core.files.uploadhandler.TemporaryFileUploadHandler"
]

You can also use the encrypted file upload handler for a specific view:

views.py

from .models import ModelWithFile
from django.core.files.uploadhandler import MemoryFileUploadHandler, TemporaryFileUploadHandler
from django.views.generic.edit import CreateView
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt, csrf_protect

@method_decorator(csrf_exempt, 'dispatch')
class CreateEncryptedFile(CreateView):
    model = ModelWithFile
    fields = ["file"]

    def post(self, request, *args, **kwargs):
        request.upload_handlers = [
            EncryptedFileUploadHandler(request=request),
            MemoryFileUploadHandler(request=request),
            TemporaryFileUploadHandler(request=request)
        ]  
        return self._post(request)

    @method_decorator(csrf_protect)
    def _post(self, request):
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

Use regular FileFields for file uploads. When you want to decrypt the file, use the EncryptedFile helper class

views.py

from .models import ModelWithFile
from encrypted_files.base import EncryptedFile
from django.http import HttpResponse

def decrypted(request,pk):
    f = ModelWithFile.objects.get(pk=pk).file
    ef = EncryptedFile(f)
    return HttpResponse(ef.read())

The EncryptedFileUploadHandler and EncryptedFile classes also take a key input if you want to use a custom key (based on the user, for example):

handler = EncryptedFileUploadHandler(request=request,key=custom_key_for_this_request)

You would then use the same key when decrypting:

ef = EncryptedFile(file,key=custom_key_for_this_request)

The EncryptedFile class is a wrapper around django's File class. It performs the decryption and counter/pointer management when .read() and .seek() are called. It can be used as a file-like object for other processing purposes, but is read-only.

How It Works

When a file is POSTed to your application, its raw byte data is passed through a series of upload handlers. The default behavior is to load the file into memory if it is small, or stream it to a temporary file if large. Then, it's moved to its "upload_to" location.

The EncryptedFileUploadHandler acts as a barrier between these default handlers, and the raw data. It prevents the unencrypted file data from being written to a temp file, by encrypting it before passing it along. It doesn't save any data, just encrypts it and passes it along.

raw bytes -> Encryption -> temp file -> final file

When the file starts the upload, the EncryptedFileUploadHandler adds 16 bytes to the start of the file. This is the nonce used to encrypt the data.

[16-byte nonce][...rest of the file (encrypted)...]
                ^ calling .seek(0) will move here

When the file needs to be decrypted, the EncryptedFile helper will read the first 16 bytes to get the nonce, then expose the rest of the file as if it starts at position 0. Methods like .seek() and .tell() are automatically corrected to make the file act like it's not encrypted at all.

Counters

When blocks are read, the counter is updated as well, based on where the internal pointer ends up. In the event of a counter overflow, it will wrap back to zero. This is the same behavior that the cryptography package uses internally.

Metadata

Release files for django-encrypted-files 0.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-encrypted-files 0.0.10
File Size Uploaded
django_encrypted_files-0.0.10.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-encrypted-files 0.0.10
File Interpreter ABI Platform
django_encrypted_files-0.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 12.4 kB

Release files / django_encrypted_files-0.0.10.tar.gz

Download URL django_encrypted_files-0.0.10.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6b2155341080b3082500bb5f93e8cfac5e40c87cda47e07667d5f4a9d47416fb
BLAKE2b-256 checksum
How to use checksums
a9d18a0b4336a8a9014291f52853277c2f6614f462c4bab074ef48310fb29c28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release files / django_encrypted_files-0.0.10-py3-none-any.whl

Download URL django_encrypted_files-0.0.10-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0be8a233da866b532fd243097f2f26b8ab0ef7f1cdeb841166a53c31973c20b7
BLAKE2b-256 checksum
How to use checksums
0a3ea02328512a9155eba64b718b75fba6eebd4fb14ed7ae667eb342b19aca01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.0.10 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page