Ruby
Ruby
Add a PFX Source for Decrypting Email
See more Email Object Examples
Demonstrates the Chilkat Email.AddPfxSourceFile method, which adds a PFX file to the internal list of certificate and private-key sources used during decryption. Call it once per PFX file; the second argument is the PFX password. This example registers a PFX source, then loads an encrypted email so Chilkat can decrypt it automatically. AddPfxSourceFile may be called one or more times, and can be called before receiving the email from an IMAP or POP3 server as well as before loading an encrypted email from a .eml file.
Background: To read an encrypted (S/MIME) email you need the recipient's private key. On Windows, Chilkat automatically searches the system certificate stores, and on macOS the Keychain — so if the key is already installed, no extra step is needed. When the private key lives in a standalone PFX file instead (common on Linux or in server deployments),
AddPfxSourceFile tells Chilkat where to find it. A PFX (PKCS#12) bundles the certificate and its private key in one password-protected file.Chilkat Ruby Downloads
require 'chilkat'
success = false
# Demonstrates the AddPfxSourceFile method, which adds a PFX file to the internal list of
# certificate and private-key sources used during decryption. Call it once per PFX file;
# the 2nd argument is the PFX password.
email = Chilkat::CkEmail.new()
# Provide a PFX containing the private key needed to decrypt the message. AddPfxSourceFile
# may be called one or more times, and should be called before the encrypted email is
# obtained -- that is, before receiving it from an IMAP or POP3 server, or (as shown here)
# before loading it from a .eml file -- so Chilkat can decrypt it automatically.
success = email.AddPfxSourceFile("qa_data/certs/decryption.pfx","pfx_password")
if (success == false)
print email.lastErrorText() + "\n";
exit
end
# Load an encrypted email; Chilkat uses the PFX source to decrypt it.
success = email.LoadEml("qa_data/eml/encrypted.eml")
if (success == false)
print email.lastErrorText() + "\n";
exit
end
print "ReceivedEncrypted = " + email.get_ReceivedEncrypted().to_s() + "\n";
print "Decrypted = " + email.get_Decrypted().to_s() + "\n";
# Note: Paths such as "qa_data/..." are relative local filesystem paths,
# relative to the current working directory of the running application.