gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:37 25 4
gpt4 key购买 nike

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

我找到了问题:Decode/extract smime.p7m file contents (email with embedded files) with OpenSSL?现在我知道如何通过命令行来完成了。

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

最佳答案

我想我应该写一个解决方案,因为我花了很长时间才弄明白。另请参阅我上面关于从何处获取的评论:

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

关于ruby - 如何使用 Ruby OpenSSL 库解码/提取 SMIME 签名电子邮件的 smime.p7m 文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7332871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com