pyaescrypt crypt词典,将密码文本保存到文件,然后读取文件和解密

发布于 2025-02-10 23:32:11 字数 1649 浏览 1 评论 0原文

因此,我正在尝试加密字典并将其保存为Cypher文本,然后打开并解密文件,以便我的程序可以使用密码。
我不想保存解密的文件,只能存储在变量中。

我的当前.py如下:

import io
import json
import pyAesCrypt


password = 'some_password'
bufferSize = 64 * 1024


def enc():
    loaded_settings = {'pass1': 'xxx',
                       'pass2': 'ccc',
                       'key': 'ddd',
                       'secret': 'sss'}

    # binary data to be encoded
    user_encode_data = json.dumps(loaded_settings).encode('utf-8')

    # input plaintext binary stream
    fIn = io.BytesIO(user_encode_data)

    # initialize ciphertext binary stream
    fCiph = io.BytesIO()

    # encrypt stream
    pyAesCrypt.encryptStream(fIn, fCiph, password, bufferSize)

    # print encrypted data
    print("This is the ciphertext:\n" + str(fCiph.getvalue()))
    with open("Output.txt", "w") as text_file:
        text_file.write(str(fCiph.getvalue()))


def dec():
    with open("Output.txt", "r") as text_file:
        cipher_text = text_file
        fCiph = io.BytesIO(cipher_text.read().encode())
    # get ciphertext length
    ctlen = len(fCiph.getvalue())

    # go back to the start of the ciphertext stream
    fCiph.seek(0)

    # initialize decrypted binary stream
    fDec = io.BytesIO()

    # decrypt stream
    pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize, ctlen)

    # print decrypted data
    print("Decrypted data:\n" + str(fDec.getvalue()))

    # decrypted data back as dict
    output_dict = json.loads(fDec.getvalue())

    print(output_dict['pass1'])


enc()
dec()

我正在收到错误valueError:文件已损坏或不是AES Crypt(或Pyaescrypt)文件。
我打开Cypher文本的方式有什么问题吗?

So I am trying to encrypt a dictionary and save it as cypher text, then open and decrypt the file so my program can use the passwords.
I do not want to save the decrypted file and it should only be stored in a variable.

My current .py is as follows:

import io
import json
import pyAesCrypt


password = 'some_password'
bufferSize = 64 * 1024


def enc():
    loaded_settings = {'pass1': 'xxx',
                       'pass2': 'ccc',
                       'key': 'ddd',
                       'secret': 'sss'}

    # binary data to be encoded
    user_encode_data = json.dumps(loaded_settings).encode('utf-8')

    # input plaintext binary stream
    fIn = io.BytesIO(user_encode_data)

    # initialize ciphertext binary stream
    fCiph = io.BytesIO()

    # encrypt stream
    pyAesCrypt.encryptStream(fIn, fCiph, password, bufferSize)

    # print encrypted data
    print("This is the ciphertext:\n" + str(fCiph.getvalue()))
    with open("Output.txt", "w") as text_file:
        text_file.write(str(fCiph.getvalue()))


def dec():
    with open("Output.txt", "r") as text_file:
        cipher_text = text_file
        fCiph = io.BytesIO(cipher_text.read().encode())
    # get ciphertext length
    ctlen = len(fCiph.getvalue())

    # go back to the start of the ciphertext stream
    fCiph.seek(0)

    # initialize decrypted binary stream
    fDec = io.BytesIO()

    # decrypt stream
    pyAesCrypt.decryptStream(fCiph, fDec, password, bufferSize, ctlen)

    # print decrypted data
    print("Decrypted data:\n" + str(fDec.getvalue()))

    # decrypted data back as dict
    output_dict = json.loads(fDec.getvalue())

    print(output_dict['pass1'])


enc()
dec()

I am getting the error ValueError: File is corrupted or not an AES Crypt (or pyAesCrypt) file.
Is there something wrong with the way I am opening the cypher text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿越时光隧道 2025-02-17 23:32:11

数据必须在二进制文件中存储和读取,否则将被损坏,即

  • eng()的末尾,必须是:
with open("Output.txt", "wb") as text_file: # Fix 1a: binary, i.e. replace w with wb
    text_file.write(fCiph.getvalue())       # Fix 2a: binary, i.e. remove str()
  • 相应地,在dec():
with open("Output.txt", "rb") as text_file: # Fix 1:  binary, ie. replace r with rb
    cipher_text = text_file
    fCiph = io.BytesIO(cipher_text.read())  # Fix 2b: binary, i.e. remove encode()

通过这些更改,解密有效。

The data must be stored and read in binary, otherwise they will be corrupted, i.e.

  • at the end of enc() it must be:
with open("Output.txt", "wb") as text_file: # Fix 1a: binary, i.e. replace w with wb
    text_file.write(fCiph.getvalue())       # Fix 2a: binary, i.e. remove str()
  • and accordingly at the beginning of dec():
with open("Output.txt", "rb") as text_file: # Fix 1:  binary, ie. replace r with rb
    cipher_text = text_file
    fCiph = io.BytesIO(cipher_text.read())  # Fix 2b: binary, i.e. remove encode()

With these changes, decryption works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文