简体   繁体   English

如何在django上传之前加密文件

[英]how to encrypt a file before uploading in django

Im building a project secure file sharing.which encrypts a file before uploading into local computer and decrypts while downloading if the user has the decryption key.I was stuck how to encrypt a file before uploading into my pc我正在构建一个项目安全文件共享。它在上传到本地计算机之前加密文件,如果用户有解密密钥,则在下载时解密。我被困在如何在上传到我的电脑之前加密文件

I'm following this approach which is mentioned below.我正在遵循下面提到的这种方法。

https://ruddra.com/documentation-of-django-encrypt-file/#basic-usage https://ruddra.com/documentation-of-django-encrypt-file/#basic-usage

but i dont't know how to link with my code.但我不知道如何与我的代码链接。 can anyone help me谁能帮我

views.py视图.py

def upload(request):
    context={}
    if request.method == 'POST':
        upload_file= request.FILES["document"]
        fs=FileSystemStorage()
        name=fs.save(upload_file.name, upload_file)
        context['url'] = fs.url(name)
    return render(request, 'accounts/upload.html',context)

upload.html上传.html

{% include 'accounts/main.html'%}

<pre>
Upload your files for sharing
</pre>


{% block content %}

  <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="document">
    <button type="submit">Upload</button>
  </form>
  {% if url %}

  <p> Uploaded file:<a href="{{ url }}">{{ url }}</a></p>
  {% endif %}

{% endblock %}

settings.py设置.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL= '/media/'

To encrypt a file BEFORE uploading to the server, it means you need to encrypt it within the browser - eg using JavaScript.要在上传到服务器之前加密文件,这意味着您需要在浏览器中对其进行加密 - 例如使用 JavaScript。 Here's a thread that can help you to encrypt stuff in JS: JavaScript AES encryption and decryption (Advanced Encryption Standard)这是一个可以帮助您在 JS 中加密内容的线程: JavaScript AES 加密和解密(高级加密标准)

If you're feeling up to the challenge, look into running AES in WASM to speed up encryption (important for large files).如果您愿意接受挑战,请考虑在 WASM 中运行 AES 以加速加密(对于大文件很重要)。

Note that in Django, all python code is executed ON THE SERVER.请注意,在 Django 中,所有 python 代码都在服务器上执行。 The linked example in the question shows how to encrypt files on the server AFTER uploading them: https://ruddra.com/documentation-of-django-encrypt-file/#basic-usage问题中的链接示例显示了如何在上传文件后加密服务器上的文件: https : //ruddra.com/documentation-of-django-encrypt-file/#basic-usage

Note that encrypting the file on the browser before uploading is only a small portion of the whole problem.请注意,在上传之前在浏览器上加密文件只是整个问题的一小部分。 To have a secure file sharing service, you would probably want to have a way to share the key with the other users who need to decrypt it.要获得安全的文件共享服务,您可能希望有一种方法可以与需要解密密钥的其他用户共享密钥。 For that you'd probably need to use asymmetric encryption, eg, wrap (encrypt) the key using other users' public keys before uploading it.为此,您可能需要使用非对称加密,例如,在上传之前使用其他用户的公钥包装(加密)密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM