Tcl
Tcl
Set the Decryption Certificate and Private Key
See more Email Object Examples
Demonstrates the Chilkat Email.SetDecryptCert2 method, which explicitly provides a certificate and its corresponding private key as separate objects for decrypting a received encrypted email. Set them before loading the encrypted message. This example loads a .cer certificate and a PEM private key, sets both, then loads an encrypted email.
Background: Sometimes the certificate and its private key are stored separately — a public
.cer file plus a PEM (or other format) key file — rather than combined in a PFX. SetDecryptCert2 accepts the two objects individually, which is the natural fit for that arrangement. It is the two-object counterpart to SetDecryptCert, which takes a single certificate that already carries its private key.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# Demonstrates the SetDecryptCert2 method, which explicitly provides a certificate and its
# corresponding private key (as separate objects) for decrypting a received encrypted
# email. Set them before loading the encrypted email.
set email [new_CkEmail]
# Load the certificate (public) and the matching private key from separate files.
set cert [new_CkCert]
set success [CkCert_LoadFromFile $cert "qa_data/certs/recipient.cer"]
if {$success == 0} then {
puts [CkCert_lastErrorText $cert]
delete_CkEmail $email
delete_CkCert $cert
exit
}
set privKey [new_CkPrivateKey]
set success [CkPrivateKey_LoadPemFile $privKey "qa_data/certs/recipient_privkey.pem"]
if {$success == 0} then {
puts [CkPrivateKey_lastErrorText $privKey]
delete_CkEmail $email
delete_CkCert $cert
delete_CkPrivateKey $privKey
exit
}
# Provide the certificate and private key to use for decryption.
set success [CkEmail_SetDecryptCert2 $email $cert $privKey]
if {$success == 0} then {
puts [CkEmail_lastErrorText $email]
delete_CkEmail $email
delete_CkCert $cert
delete_CkPrivateKey $privKey
exit
}
# Load the encrypted email; Chilkat decrypts it using the certificate and key.
set success [CkEmail_LoadEml $email "qa_data/eml/encrypted.eml"]
if {$success == 0} then {
puts [CkEmail_lastErrorText $email]
delete_CkEmail $email
delete_CkCert $cert
delete_CkPrivateKey $privKey
exit
}
puts "Decrypted = [CkEmail_get_Decrypted $email]"
# Note: Paths such as "qa_data/..." are relative local filesystem paths,
# relative to the current working directory of the running application.
delete_CkEmail $email
delete_CkCert $cert
delete_CkPrivateKey $privKey