pyaescrypt crypt词典,将密码文本保存到文件,然后读取文件和解密
因此,我正在尝试加密字典并将其保存为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据必须在二进制文件中存储和读取,否则将被损坏,即
eng()
的末尾,必须是:dec():
通过这些更改,解密有效。
The data must be stored and read in binary, otherwise they will be corrupted, i.e.
enc()
it must be:dec()
:With these changes, decryption works.