如何使用 *.pub/*.sec 文件到 encrypt/decrypt 另一个文件?

How to use *.pub/*.sec files to encrypt/decrypt another file?

我使用此处给出的说明和代码创建了一对 *.pub*.sec 文件:

https://www.gnupg.org/documentation/manuals/gnupg/Unattended-GPG-key-generation.html

(我正在使用此文档,因为我拥有的最终应用程序 mind 是一个自动化的 encryption/decryption 管道。)

Q1:如何使用gpg2和*.pub文件加密另一个文件?

Q2: 我如何使用 gpg2 和伴侣 *.sec 来解密使用伴侣 *.pub 文件加密的文件?


重要提示: 我只对适合以编程方式实现无监督操作的答案感兴趣。请不要post只能进行交互的回答。我对可以在 Python.

中实施的解决方案特别感兴趣

请附上相关文档的精确指针。

看的时候encrypting and decrypting documents

这提示 pexpect;虽然我可以提供常规 expect 脚本:

这不是直接的 Python 解决方案,但应该很容易移植。

如标语所示:

Pexpect makes Python a better tool for controlling other applications.

加密:

gpg --output doc.gpg --encrypt --recipient blake@cyb.org doc

作为expect脚本;用法 ./encrypt.exp doc blake@cyb.org 1234(注意 : 之后的 space):

#!/usr/bin/expect -f
set filename [lindex $argv 0]
set recipient [lindex $argv 1]
set passphrase [lindex $argv 2]

spawn gpg --output $filename.gpg --encrypt --recipient $recipient $filename
expect -exact "Enter pass phrase: "
send -- "$passphrase\r"
expect eof

解密:

gpg --output doc --decrypt doc.gpg

作为expect脚本;用法:./decrypt.exp doc 1234

#!/usr/bin/expect -f
set filename [lindex $argv 0]
set passphrase [lindex $argv 1]

spawn gpg --output $filename --decrypt $filename.gpg
expect -exact "Enter pass phrase: "
send -- "$passphrase\r"
expect eof

导入:

钥匙可以导入到任一钥匙链中:

gpg --import somekey.sec
gpg --list-secret-keys

gpg --import somekey.pub
gpg --list-keys

几乎没有什么可以自动化的;但是,将导入的密钥设置为 "trusted" 需要 expect 才能实现自动化。找到这个 cheat-sheet,它在一页上包含所有命令;它还提示:If you have multiple secret keys, it'll choose the correct one, or output an error if the correct one doesn't exist(这应该证实我在下面的评论)。

文件~/.gnupg/options是用户的options file;在哪里可以,例如。定义默认密钥服务器。

关于你所说的一些信息:

I created a pair of *.pub and *.sec files using the instructions

非常适合与正在交换信息的人共享 public 密钥,但从技术上讲,当您以编程方式工作时,不需要直接使用这些文件。

需注意:

  • 当您加密数据时,您将指定与用于加密的密钥相对应的收件人
  • 当您解密数据时,您将首先导入所有者的 public 密钥,然后您就可以在不指定接收者的情况下解密数据,因为它嵌入在加密数据中

Actually, I am somewhat confused on this question. I have read conflicting information [...]

我同意这很令人困惑。这种情况,我觉得还是用版本1比较好,经验比较多,找第三方库用。

在这个回答中,我试过:

  • python-gnupg(对于 GnuPG v1)这是一个众所周知的 Python 库,完全符合您的需求
  • cryptorito(对于 GnuPG v2)我没有找到足够的文档

有了第一个库,您可以简单地将它安装到您的系统中:

sudo pip install python-gnupg

然后编写一个Python脚本来执行你想要的所有操作。

我写了一个简单的来回答你的问题。

#!/bin/python

import gnupg

GPG_DIR='/home/bsquare/.gnupg'
FILE_TO_ENCRYPT='/tmp/myFileToEncrypt'
ENCRYPTED_FILE='/tmp/encryptedFile'
DECRYPTED_FILE='/tmp/decryptedFile'
SENDER_USER='Bsquare'
TARGET_USER='Kjo'

gpg = gnupg.GPG(gnupghome=GPG_DIR)

print("Listing keys ...")
print(gpg.list_keys())

# On SENDER_USER side ... encrypt the file for TARGET_USER, with his public key (would match the kjo.pub if the key was exported).
print("Encrypting file " + FILE_TO_ENCRYPT + " for " + TARGET_USER + " ...")
with open(FILE_TO_ENCRYPT, "rb") as sourceFile:
    encrypted_ascii_data = gpg.encrypt_file(sourceFile, TARGET_USER)
    # print(encrypted_ascii_data)
    with open(ENCRYPTED_FILE, "w+") as targetFile:
        print("encrypted_ascii_data", targetFile)


# On TARGET_USER side ... decrypt the file with his private key (would match the kjo.sec if the key was exported).
print("Decrypting file " + ENCRYPTED_FILE + " for " + TARGET_USER + " ...")
with open(ENCRYPTED_FILE, "rb") as sourceFile:
    decrypted_ascii_data = gpg.decrypt_file(sourceFile)
    # print(decrypted_ascii_data)
    with open(DECRYPTED_FILE, "w+") as targetFile:
        print(decrypted_ascii_data, targetFile)

请注意,我的密钥环包含 Bsquare 用户的 pub/sec 对,以及 Kjo 用户的公钥。

Since version 2.1.14, GPG supports the --recipient-file option, which lets you specify the public key to encrypt with without using the keyring. To quote the developer:

It is now possible to bypass the keyring and take the public key directly from a file. That file may be a binary or an ascii armored key and only the first keyblock from that file is used. A key specified with this option is always fully trusted.

This option may be mixed with the standard -r options. --hidden-recipient-file (or -F) is also available.

To futher assist some use cases the option

--no-keyring

has also been implemented. This is similar to

--no-default-keyring --keyring /dev/null

but portable to Windows and also ignores any keyring specified (command line or config file).

所以要加密,您可以这样做:

gpg --output myfileenc --encrypt --recipient-file key.pub myfile

要自动化,除了如其他答案中所述使用 expect 或 Python 之外,您还可以 use the --batch option。 (您需要查看提供的答案中哪个最适合您的系统)。

但是,没有这样的选项可用于密钥,事实上,--generate-key 命令中的 PGP (2.1) deprecated the secring option 版本相同,所以这个文件甚至不再可用。生成的密钥需要添加到密钥环以用于解密。