如何使用 S/MIME 加密电子邮件

如何使用 S/MIME 加密电子邮件

我正在考虑使用 S/MIME v3.1 加密我的一些邮件,因为我的设备应该支持它。

现在我想知道是否有一个(命令行)工具可以为我做到这一点?我尝试用谷歌搜索,但没有找到任何有用的信息。

答案1

是的,OpenSSL(openssl smimeopenssl cms)可以做到这一点:

man smime

描述

smime 命令处理 S/MIME 邮件。它可以加密、解密、签名和验证 S/MIME 消息。

man cms

描述

cms 命令处理 S/MIME v3.1 邮件。它可以加密、解密、签名和验证、压缩和解压缩 S/MIME 消息。

答案2

这是一个用于加密现有电子邮件的脚本,但您需要以 MH 格式的文件形式访问它们。作为额外奖励,如果第二个参数是 GPG 密钥 ID,它将使用 GPG 而不是 S/MIME。如果第二个参数是指向以 .pem 结尾的文件的路径,则脚本假定第二个参数是 pem 格式的 X509 证书,其对应的私钥最终将用于解密电子邮件。

#!/usr/bin/awk -f

## Encrypt emails in MH format.

## 1st argument is email file to encrypt.
## 2nd argument is PGP key identifier, or for S/MIME, certificate file.

BEGIN {
        ## If second argument ends with .pem, assume that S/MIME output
        ## is required, otherwise assume PGP/MIME.
        if (ARGC == 3 && ARGV[2] ~ /\.pem$/) S = 1 ## S/MIME, not PGP

        if (S == 1) {
                Encrypt = "openssl smime -encrypt -aes256 -outform pem " ARGV[2]
                Encrypt = Encrypt "|sed '/^-----BEGIN PKCS7-----/d;"
                Encrypt = Encrypt "/^-----END PKCS7-----/d'"}
        else {
                Encrypt = "gpg2 --armor --encrypt -r " ARGV[2]
                Random = "openssl rand -base64 30"}

        for (i=2;i < ARGC;i++) delete ARGV[i]        ## Just one input file.
}

{
        sub(/\r$/,"",$0)}

##==========================================================

BlankCount > 0 {           ## Everything from the 1st blank line onwards:
        print $0 | Encrypt ## Pipe opened on 1st matching line; stays open.
        next}

##----------------------------------------------------------

$0 ~ /^[^ \t]/ {        ## Any line starting with a non-whitespace character.
        CurrentBlank = 0
        if (Started == 0) Started = 1}

##----------------------------------------------------------

$0 ~ /^[ \t]*$/ {        ## Blank line NOT at the top of the file.
        if (CurrentBlank == 0 && Started == 1) BlankCount++
        CurrentBlank = 1

        ## New Content-Type and Content-Transfer-Encoding headers to go at the
        ## end of the header-block, i.e. before the first blank line:
        if (BlankCount == 1) {
                if (S == 1) {
                        H = "Content-Type: application/pkcs7-mime;"
                        H = H " name=\"smime.p7m\"; smime-type=enveloped-data\n"
                        H = H "Content-Transfer-Encoding: base64\n"
                        H = H "Content-Disposition: attachment;"
                        H = H " filename=\"smime.p7m\"\n"
                        H = H "Content-Description: S/MIME Encrypted Message"}
                else {
                        Random | getline Boundary
                        Boundary = "Encrypt_/" Boundary

                        H = "Content-Type: multipart/encrypted;"
                        H = H "\n boundary=\"" Boundary "\";"
                        H = H "\n protocol=\"application/pgp-encrypted\"\n\n"

                        H = H "--" Boundary "\n"
                        H = H "Content-Type: application/pgp-encrypted\n\n"

                        H = H "Version: 1\n\n"

                        H = H "--" Boundary "\n"
                        H = H "Content-Type: application/octet-stream\n"}

                print H

                printf("%s\n", ContentType) | Encrypt
                printf("%s\n\n", TransferEncoding) | Encrypt}}

##----------------------------------------------------------

## Save original Content-Type and Content-Transfer-Encoding to put in
## encrypted part:

tolower($0) ~ /^content-type[ \t]*:/ {
        ContentType = $0
        sub(/[^:][^:]*: */,"",ContentType)
        ContentType = "Content-Type: " ContentType
        ContentTypeLineNumber = FNR
        next}
tolower($0) ~ /^content-transfer-encoding[ \t]*:/ {
        TransferEncoding = $0
        TransferEncoding = "Content-Transfer-Encoding: " TransferEncoding
        sub(/[^:][^:]*: */,"",TransferEncoding)
        TransferEncodingLineNumber = FNR
        next}

$0 ~ /^[ \t][ \t]*[^ \t]/ {        ## Non-blank line starting with space or tab
        CurrentBlank = 0
        if (BlankCount == 0 && FNR > 1) {
                ## This must be a continuation line in the header
                if (FNR - 1 == ContentTypeLineNumber) {
                        ContentTypeLineNumber = FNR
                        ContentType = ContentType "\n" $0
                        next}
                if (FNR - 1 == TransferEncodingLineNumber) {
                        TransferEncodingLineNumber = FNR
                        TransferEncoding = TransferEncoding "\n" $0
                        next}}}

##----------------------------------------------------------

Started == 1 {                ## All header lines other than Type and Encoding.
        print $0}

END {
        close(Encrypt)
        if (S == 1) print ""
        else printf("\n--%s--\n", Boundary)}

##----------------------------------------------------------

相关内容