如何使用 Ruby OpenSSL 库解码/提取 SMIME 签名电子邮件的 smime.p7m 文件内容

发布于 2024-12-03 06:02:45 字数 358 浏览 1 评论 0 原文

我有一封签名电子邮件作为字符串。我想获取带有完整未签名消息的字符串,其中包含附件和正文,我可以使用它进行解析,例如 Mail gem。

我发现问题: 解码/提取 smime .p7m 文件内容(带有嵌入文件的电子邮件)与 OpenSSL? 现在我知道如何通过命令行执行此操作。

我可以将字符串转储到临时文件,通过命令行解密,然后解析它。但这不是一个好主意。我想为 Ruby 使用 OpenSSL 库。

I have a signed email message as string. And I want to get string with whole unsigned message with attachments and body that I can parse with, for example, Mail gem.

I found question: Decode/extract smime.p7m file contents (email with embedded files) with OpenSSL? and now I know how to do it via command line.

I can dump my string to temp file, decrypt via command line and then parse it. But this is not so good idea. I want to use OpenSSL library for Ruby.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山人契 2024-12-10 06:02:45

我想我应该写一个解决方案,因为我花了很长时间才弄清楚这个问题。另请参阅我上面的评论,了解此内容的来源:

include OpenSSL

# assuming that mail contains the message that you have likely fetched with the mail gem
data = mail.to_s

# load your certificate and key
# if you need to convert from a .p12 file for example 
# check this https://stackoverflow.com/questions/13732826/convert-pem-to-crt-and-key
cert = X509::Certificate.new(File::read("your_cert.cer"))
key = PKey::RSA.new(File::read("your.key"))

# load the encrypted mail
p7enc = PKCS7::read_smime(data)

# this is the plain email that you can read back into the mail gem and extract the required data
Mail.read_from_string(p7enc.decrypt(key, cert))

I thought I'd write up a solution, because it took me quite some time to figure this out. Also see my comment above on where this was taken from:

include OpenSSL

# assuming that mail contains the message that you have likely fetched with the mail gem
data = mail.to_s

# load your certificate and key
# if you need to convert from a .p12 file for example 
# check this https://stackoverflow.com/questions/13732826/convert-pem-to-crt-and-key
cert = X509::Certificate.new(File::read("your_cert.cer"))
key = PKey::RSA.new(File::read("your.key"))

# load the encrypted mail
p7enc = PKCS7::read_smime(data)

# this is the plain email that you can read back into the mail gem and extract the required data
Mail.read_from_string(p7enc.decrypt(key, cert))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文