ValueError at / Fernet key must be 32 url-safe base64-encoded bytes. продолжает давать мне проблемы
Я получаю эту ошибку уже довольно давно. Я пытаюсь расшифровать текстовое содержимое, которое было зашифровано с помощью fernet. Ключ передается через элемент ввода в моем шаблоне django. После получения значения из текстового ввода, я попытался использовать его для расшифровки текстового содержимого, но я продолжал получать эту ошибку. Ниже приведен мой код
мой код шаблона django:
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<h3>Welcome to Encryption, Decryption and Compression program using Python</h3>
{% for field in form %}
<div>{{ field }} </div>
{% endfor %}
<select name="choices" id="choices">
<option>Encryption</option>
<option>Decryption</option>
<option>Encryption and Compression</option>
<option>Decryption and Compression</option>
<option>Compression</option>
</select>
<input type="text" name="dkey" id="dkey" placeholder="Enter Decryption Key">
<button >Proceed</button>
</form>
my views.py
def decryption(self, key):
key = base64.urlsafe_b64encode(bytes(key, encoding="utf-8"))
f = Fernet(key)
def post(self, request):
m_file = request.FILES['file']
opt = request.POST.get('choices')
if opt == "Decryption":
dkey = request.POST.get('dkey')
decrypted = self.decryption(dkey)
ошибка, которую я получаю:
ValueError at /
Fernet key must be 32 url-safe base64-encoded bytes.
клавиша fernet, введенная через текстовый ввод:
**Pl85l36CA7TJe48jv8nWYFD4SWIhIJNFQL8CyyLlXR4=**
Вот метод шифрования:
def encryption(self, content):
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(bytes(str(content), encoding='utf-8'))
return cipher_text, key