Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Go Examples
Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Go) Decrypt PKCS7 (CMS) EnvelopedData

This example receives a PKCS7 (CMS) EnvelopedData message that was encrypted to me. To decrypt it:

  1. 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
  2. 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.
  3. 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.

Note: This example requires Chilkat v11.0.0 or greater.

Chilkat Go Downloads

Go Package for Windows, MacOS, Linux, Alpine Linux, Solaris

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

    sb := StringBuilder_Ref.html">chilkat.NewStringBuilder()
    bCrlf := true
    sb.AppendLine("MIICSgYJKoZIhvcNAQcDoIICOzCCAjcCAQAxggHiMIIB3gIBADCBljCBgTELMAkGA1UEBhMCSVQx",bCrlf)
    sb.AppendLine("EDAOBgNVBAgMB0JlcmdhbW8xGTAXBgNVBAcMEFBvbnRlIFNhbiBQaWV0cm8xFzAVBgNVBAoMDkFj",bCrlf)
    sb.AppendLine("dGFsaXMgUy5wLkEuMSwwKgYDVQQDDCNBY3RhbGlzIENsaWVudCBBdXRoZW50aWNhdGlvbiBDQSBH",bCrlf)
    sb.AppendLine("MwIQPCWvkSv8oQ7xRmEHJ6TzEDA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAQUAoRwwGgYJ",bCrlf)
    sb.AppendLine("KoZIhvcNAQEIMA0GCWCGSAFlAwQCAQUABIIBAKqHAPQNSsQoX7B2NH7QyEOWQRsSVs8oCHXmy8f4",bCrlf)
    sb.AppendLine("MVZD2er3bvYUCIomxpwbLEAl14qjUIMynahooYGgqip7+4FqL301G+BVjZVfEhHWj+VI1dAWnWuL",bCrlf)
    sb.AppendLine("VHlvc/pbQNBWqV8rKVJsNIsuAZkdj4WSwLVKxYkYX43B8fh/g71XN2DTJu7Z/824v48KBmgpQBOT",bCrlf)
    sb.AppendLine("2q7IcDGxNPAFN2p6eavIVGn2LvhEbf/Fszyj+GR5tMcnQP1BOLJ3s3JzUBbvj8hcZrF1Vhl9HnTU",bCrlf)
    sb.AppendLine("YQx8G/KdW1mR+Wlhl3BWoK0LYKRTbnTx2BXOs0CY1SXOAdhKr01ZYjA+xW4nGzY0lfXS9QZjh9gw",bCrlf)
    sb.AppendLine("TAYJKoZIhvcNAQcBMB0GCWCGSAFlAwQBKgQQw0xTbfmnt0zjWHo5SaQIp4AgxTVY9E/Ncqy6t+RM",bCrlf)
    sb.AppendLine("8y4c3Av62/wB8IpPUEmtM2OeuZo=",bCrlf)

    crypt := Crypt2_Ref.html">chilkat.NewCrypt2()

    crypt.SetCryptAlgorithm("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.

    crypt.SetEncodingMode("base64")
    crypt.SetCharset("utf-8")

    decryptedText := crypt.DecryptStringENC(*sb.GetAsString())
    if crypt.LastMethodSuccess() == false {
        fmt.Println(crypt.LastErrorText())
        sb.DisposeStringBuilder()
        crypt.DisposeCrypt2()
        return
    }

    fmt.Println(*decryptedText)

    // We can get the digital certificate used for decryption by calling LastDecryptCert
    cert := Cert_Ref.html">chilkat.NewCert()
    success := crypt.LastDecryptCert(cert)
    if crypt.LastMethodSuccess() == false {
        fmt.Println(crypt.LastErrorText())
        sb.DisposeStringBuilder()
        crypt.DisposeCrypt2()
        cert.DisposeCert()
        return
    }

    fmt.Println("Cert_Ref.html">Cert used to decrypt: ", cert.SubjectCN())

    sb.DisposeStringBuilder()
    crypt.DisposeCrypt2()
    cert.DisposeCert()

 

© 2000-2025 Chilkat Software, Inc. All Rights Reserved.