Swift
Swift
Set Cert and Private Key for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: Sometimes the certificate (the public part) and the private key are stored separately — a
.cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
// separately supplied private key for decrypting S/MIME messages. The 1st argument is the
// Cert and the 2nd is the PrivateKey.
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
success = imap.connect(hostname: "imap.example.com")
if success == false {
print("\(imap.lastErrorText!)")
return
}
success = imap.login(login: "user@example.com", password: "myPassword")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Load the certificate and its private key from separate files.
let cert = CkoCert()!
success = cert.load(fromFile: "qa_data/smime.cer")
if success == false {
print("\(cert.lastErrorText!)")
return
}
let privKey = CkoPrivateKey()!
success = privKey.loadPemFile(path: "qa_data/smime_privkey.pem")
if success == false {
print("\(privKey.lastErrorText!)")
return
}
// Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
success = imap.setDecryptCert2(cert: cert, key: privKey)
if success == false {
print("\(imap.lastErrorText!)")
return
}
success = imap.disconnect()
if success == false {
print("\(imap.lastErrorText!)")
return
}
}