PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkCert.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; 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.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Load the certificate and its private key from separate files.
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkCert::ckLoadFromFile(cert,"qa_data/smime.cer")
If success = 0
Debug CkCert::ckLastErrorText(cert)
CkImap::ckDispose(imap)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
privKey.i = CkPrivateKey::ckCreate()
If privKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPrivateKey::ckLoadPemFile(privKey,"qa_data/smime_privkey.pem")
If success = 0
Debug CkPrivateKey::ckLastErrorText(privKey)
CkImap::ckDispose(imap)
CkCert::ckDispose(cert)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
; Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
success = CkImap::ckSetDecryptCert2(imap,cert,privKey)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkCert::ckDispose(cert)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkCert::ckDispose(cert)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkCert::ckDispose(cert)
CkPrivateKey::ckDispose(privKey)
ProcedureReturn
EndProcedure