Sample code for 30+ languages & platforms
PowerBuilder

Create PKCS7 (CMS) EnvelopedData

Encrypt some data to a recipient by creating a PKCS7 (CMS) EnvelopedData structure. The data will be encrypted using a symmetric content-encryption algorithm (e.g., AES), and the randomly generated symmetric key will be encrypted using the recipient’s RSA public key extracted from their X.509 certificate.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Crypt
oleobject loo_Cert
string ls_ToBeEncrypted
string ls_Result

li_Success = 0

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")
if li_rc < 0 then
    destroy loo_Crypt
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Specify the encryption to be used.
// "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped-data) message.
loo_Crypt.CryptAlgorithm = "pki"
loo_Crypt.Pkcs7CryptAlg = "aes"
loo_Crypt.KeyLength = 256
loo_Crypt.OaepHash = "sha256"
loo_Crypt.OaepPadding = 1

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

// Use a certificate found in the Windows certificate store.
li_Success = loo_Cert.LoadByCommonName("My Certificate")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    return
end if

// Tell the crypt object to use the certificate.
loo_Crypt.SetEncryptCert(loo_Cert)

ls_ToBeEncrypted = "This string is to be encrypted."

// Get the result in multi-line BASE64 MIME format.
loo_Crypt.EncodingMode = "base64_mime"
loo_Crypt.Charset = "utf-8"

ls_Result = loo_Crypt.EncryptStringENC(ls_ToBeEncrypted)
if li_Success <> 1 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Crypt
    destroy loo_Cert
    return
end if

// -------------------------------------------------------------------------
// See the following example to decrypt what was created in this example
// Decrypt PKCS7 (CMS) EnvelopedData
// -------------------------------------------------------------------------

Write-Debug ls_Result

// Sample output:

// 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=


destroy loo_Crypt
destroy loo_Cert