4

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.

4

1 回答 1

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))
于 2018-08-24T11:12:42.623 回答