直接写入Python中的加密文件
在python中,使用gnupg包,是否可以在内存中获取一个值,然后将其写入加密文件,而不是写入文件然后进行加密 我希望这样的事情能奏效:直接写入Python中的加密文件,python,gnupg,Python,Gnupg,在python中,使用gnupg包,是否可以在内存中获取一个值,然后将其写入加密文件,而不是写入文件然后进行加密 我希望这样的事情能奏效: import gnupg gpg = gnupg.GPG(gnupghome='keydirectory') l = [['a', '1'], ['b', '2'], ['c', '3']] gpg.encrypt_file(l, recipients=['test@test.com'], output='encryptedfile.asc') 我希
import gnupg
gpg = gnupg.GPG(gnupghome='keydirectory')
l = [['a', '1'], ['b', '2'], ['c', '3']]
gpg.encrypt_file(l, recipients=['test@test.com'], output='encryptedfile.asc')
我希望有一个这样的写作概念可以一行一行地循环,但我找不到
with open('regularfile.txt', 'w') as file:
for i in l:
file.write(i)
理想情况下,我可以连接到数据库并通过直接写入输出文件 只需使用
gpg.encrypt()
函数而不是encrypt\u file()
,然后写入文件。只需使用gpg.encrypt()
函数而不是encrypt\u file()
然后写入文件。这就是您想要的
导入gnupg
>>>gpg=gnupg.gpg(homedir=“doctests”)
>>>按键设置=gpg.gen按键输入(
…密钥_type='RSA',
…键长度=1024,
…键的用法为“ESCA”,
…密码短语class='foo')
>>>键=gpg.gen\u键(键设置)
>>>message=“乌鸦在午夜飞翔。”
>>>encrypted=str(gpg.encrypt(message,key.printprint))
>>>断言加密!=消息
>>>断言未加密。isspace()
>>>decrypted=str(gpg.decrypt(加密))
>>>断言未解密。isspace()
>>>解密
“乌鸦在午夜飞。”
显示的就是您想要的
导入gnupg
>>>gpg=gnupg.gpg(homedir=“doctests”)
>>>按键设置=gpg.gen按键输入(
…密钥_type='RSA',
…键长度=1024,
…键的用法为“ESCA”,
…密码短语class='foo')
>>>键=gpg.gen\u键(键设置)
>>>message=“乌鸦在午夜飞翔。”
>>>encrypted=str(gpg.encrypt(message,key.printprint))
>>>断言加密!=消息
>>>断言未加密。isspace()
>>>decrypted=str(gpg.decrypt(加密))
>>>断言未解密。isspace()
>>>解密
“乌鸦在午夜飞。”