【问题标题】:How to Decode/extract smime.p7m file contents of SMIME signed email using Ruby OpenSSL library如何使用 Ruby OpenSSL 库解码/提取 SMIME 签名电子邮件的 smime.p7m 文件内容
【发布时间】:2011-11-12 01:40:50
【问题描述】:

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

我发现了问题:Decode/extract smime.p7m file contents (email with embedded files) with OpenSSL?,现在我知道如何通过命令行进行操作了。

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

【问题讨论】:

标签: ruby openssl signed smime


【解决方案1】:

我想我会写一个解决方案,因为我花了很长时间才弄清楚这一点。另请参阅我上面的评论,了解这是从哪里获取的:

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

【讨论】:

    猜你喜欢
    • 2010-10-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2021-07-19
    • 1970-01-01
    • 2017-04-05
    相关资源
    最近更新 更多