PowerBuilder
PowerBuilder
Decrypt PKCS7 (CMS) EnvelopedData
This example receives a PKCS7 (CMS) EnvelopedData message that was encrypted to me. To decrypt it:
- The CMS structure contains -
- Encrypted content (ciphertext)
- The symmetric key used to encrypt the content, itself encrypted with my public key
- Information identifying the recipient (me), typically by X.509 certificate
- Decryption process (what Chilkat does internally)
- My software uses my digital certificat's private RSA key (which corresponds to the public key used during encryption) to decrypt the encrypted symmetric key.
- The decrypted symmetric key is then used to decrypt the content-encryption layer, typically encrypted with a symmetric algorithm like AES.
- Result -
- I recover the original plaintext data.
Summary:
- The content itself is encrypted with a symmetric key (e.g., AES).
- That symmetric key is encrypted using my RSA public key and included in the EnvelopedData.
- I use my RSA private key to decrypt the symmetric key and then use it to decrypt the actual data.
Only the intended recipient, who possesses the correct private key, can successfully decrypt the EnvelopedData.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sb
integer li_BCrlf
oleobject loo_Crypt
string ls_DecryptedText
oleobject loo_Cert
li_Success = 0
// Here's the PKCS7 (CMS) EnvelopedData we received.
// It was created using this example: Create PKCS7 CMS EnvelopedData
// MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx
// EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj
// dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH
// MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ
// KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4
// MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL
// VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT
// 2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU
// YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw
// TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM
// 8y4c3Av62/wB8IpPUEmtM2OeuZo=
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
li_BCrlf = 1
loo_Sb.AppendLine("MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx",li_BCrlf)
loo_Sb.AppendLine("EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj",li_BCrlf)
loo_Sb.AppendLine("dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH",li_BCrlf)
loo_Sb.AppendLine("MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ",li_BCrlf)
loo_Sb.AppendLine("KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4",li_BCrlf)
loo_Sb.AppendLine("MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL",li_BCrlf)
loo_Sb.AppendLine("VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT",li_BCrlf)
loo_Sb.AppendLine("2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU",li_BCrlf)
loo_Sb.AppendLine("YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw",li_BCrlf)
loo_Sb.AppendLine("TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM",li_BCrlf)
loo_Sb.AppendLine("8y4c3Av62/wB8IpPUEmtM2OeuZo=",li_BCrlf)
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
loo_Crypt.CryptAlgorithm = "pki"
// The CMS message contains information about the digital certificate that was used to encrypt.
// Assuming the certificate is installed in a Windows certificate store, or macOS keychain,
// Chilkat will automatically locate and use the certificate + private key for decryption.
loo_Crypt.EncodingMode = "base64"
loo_Crypt.Charset = "utf-8"
ls_DecryptedText = loo_Crypt.DecryptStringENC(loo_Sb.GetAsString())
if loo_Crypt.LastMethodSuccess = 0 then
Write-Debug loo_Crypt.LastErrorText
destroy loo_Sb
destroy loo_Crypt
return
end if
Write-Debug ls_DecryptedText
// We can get the digital certificate used for decryption by calling LastDecryptCert
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Crypt.LastDecryptCert(loo_Cert)
if loo_Crypt.LastMethodSuccess = 0 then
Write-Debug loo_Crypt.LastErrorText
destroy loo_Sb
destroy loo_Crypt
destroy loo_Cert
return
end if
Write-Debug "Cert used to decrypt: " + loo_Cert.SubjectCN
destroy loo_Sb
destroy loo_Crypt
destroy loo_Cert