在Python中直接写入加密文件

2024-04-28 08:29:26 发布

您现在位置:Python中文网/ 问答频道 /正文

在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')

我希望有一个像这样的书写概念可以逐行循环,但我找不到。在

^{pr2}$

理想情况下,我可以连接到数据库并通过直接写入输出文件。在


Tags: 文件内存testimportcom事情gpgfile
2条回答

documentation for the ^{} method表示这是您想要的。在

>>> import gnupg

>>> gpg = gnupg.GPG(homedir="doctests")
>>> key_settings = gpg.gen_key_input(
...         key_type='RSA',
...         key_length=1024,
...         key_usage='ESCA',
...         passphrase='foo')
>>> key = gpg.gen_key(key_settings)

>>> message = "The crow flies at midnight."
>>> encrypted = str(gpg.encrypt(message, key.printprint))
>>> assert encrypted != message
>>> assert not encrypted.isspace()

>>> decrypted = str(gpg.decrypt(encrypted))
>>> assert not decrypted.isspace()
>>> decrypted
'The crow flies at midnight.'

只需使用gpg.encrypt()函数而不是encrypt_file(),然后写入一个文件。在

相关问题 更多 >