Swift Requires Chilkat v11.0.0+
Swift
Create an encrypted S/MIME file without SMTP
See more Email Object Examples
Encrypt a saved draft for a recipient with an RSA encryption certificate. This example uses AES-256-GCM for the content and RSA-OAEP for the content-encryption key. Confirm these choices are supported by the receiving mail client. RenderToMimeBd produces the protected bytes without submitting mail.
Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let email = CkoEmail()!
success = email.loadEml(mimePath: "message.eml")
if success != true {
print("\(email.lastErrorText!)")
return
}
// Use the recipient's public encryption certificate. Its private key stays with the recipient.
let cert = CkoCert()!
success = cert.load(fromFile: "recipient.cer")
if success != true {
print("\(cert.lastErrorText!)")
return
}
success = email.setEncryptCert(cert: cert)
if success != true {
print("\(email.lastErrorText!)")
return
}
email.pkcs7CryptAlg = "aes-gcm"
email.pkcs7KeyLength = 256
email.oaepPadding = true
email.oaepHash = "sha256"
email.oaepMgfHash = "sha256"
email.sendEncrypted = true
let mail = CkoMailMan()!
let rendered = CkoBinData()!
success = mail.render(toMimeBd: email, renderedMime: rendered)
if success != true {
print("\(mail.lastErrorText!)")
return
}
success = rendered.writeFile(path: "encrypted.eml")
if success != true {
print("Failed: rendered.WriteFile.")
return
}
print("Saved encrypted.eml.")
}