Sample code for 30+ languages & platforms
PowerBuilder

PKCS7 Encrypt using Multiple Certificates

See more MIME Examples

Encrypts MIME using multiple digital certificates. Calls AddEncryptCert once per certificate, followed by a call to EncryptN. The MIME may be decrypted by any owner of one of the certificates + its associated private key.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Mime
oleobject loo_Cert1
oleobject loo_Cert2

li_Success = 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

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

//  Build a simple MIME message to be encrypted:
li_Success = loo_Mime.AddHeaderField("Content-Type","text/plain")
li_Success = loo_Mime.AddHeaderField("abc","123")
loo_Mime.SetBody("This is a test")

//  A digital certificate is required to create PKCS7 encrypted MIME.
//  It can come from a variety of sources: .cer file, .pfx file, PEM files,
//  an in-memory representation, or directly from a Windows
//  registry-based certificate store.

//  Load one cert from a .cer file:
loo_Cert1 = create oleobject
li_rc = loo_Cert1.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert1.LoadFromFile("myCert.cer")
if li_Success = 0 then
    Write-Debug loo_Cert1.LastErrorText
    destroy loo_Mime
    destroy loo_Cert1
    return
end if

//  Load another cert ...
loo_Cert2 = create oleobject
li_rc = loo_Cert2.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert2.LoadFromFile("anotherCert.cer")
if li_Success = 0 then
    Write-Debug loo_Cert2.LastErrorText
    destroy loo_Mime
    destroy loo_Cert1
    destroy loo_Cert2
    return
end if

//  Tell the MIME object to use these certs for encrypting:
//  (AddEncryptCert shouldn't fail if we already know it contains
//  a valid certificate..)
li_Success = loo_Mime.AddEncryptCert(loo_Cert1)
li_Success = loo_Mime.AddEncryptCert(loo_Cert2)

//  Encrypt the MIME:
li_Success = loo_Mime.EncryptN()
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    destroy loo_Cert1
    destroy loo_Cert2
    return
end if

//  Display the MIME:
Write-Debug loo_Mime.GetMime()

//  The resulting S/MIME looks something like this:

//  abc: 123
//  Content-Disposition: attachment; filename="smime.p7m";
//  Content-Transfer-Encoding: base64
//  Content-Type: application/x-pkcs7-mime;
//   name="smime.p7m";
//  
//  MIID4QYJKoZIhvcNAQcDoIID0jCCA84CAQAxggNlMIIBgQIBADBpMFUxCzAJBgNVBAYTAlpBMSUw
//  IwYDVQQKExxUaGF3dGUgQ29uc3VsdGluZyAoUHR5KSBMdGQuMR8wHQYDVQQDExZUaGF3dGUgQ29k
//  ZSBTaWduaW5nIENBAhB4ouTcAmLszrGi170k1deSMA0GCSqGSIb3DQEBAQUABIIBAKnOPgmISrNH
//  Hd+zESvH5dl5TvBLCxLuIuH6cuZdowHzCqAUzaYFdn1rp88Vvu2x3s6WOvzje/+dE1x0YjEpeqxd
//  ufehlb6Jl9ucxSMWARWMxWGxu0eZGJVt+SBBrVddqdtrIU9lVt5rBxkU67P8PxgbwdNCX3qJENRO
//  P/rvp5ngoCkpIxeGqSkPvWv+CA3IXS8FPs8Sl4HNPiAj3e9RBm2MxI1u4cn1HvgGg8yHgHLMG5JF
//  LzGsmotSQaqEwq2JtOlkjfOwEI+ZhbWQupj6gen2aGc5sW3J26H1usxtV2TQ7WRRxqnZGmlh20dW
//  4lo3kITYI+iapGlJXmrlDYh2CjkwggHcAgEAMIHDMIGuMQswCQYDVQQGEwJVUzELMAkGA1UECBMC
//  VVQxFzAVBgNVBAcTDlNhbHQgTGFrZSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdv
//  cmsxITAfBgNVBAsTGGh0dHA6Ly93d3cudXNlcnRydXN0LmNvbTE2MDQGA1UEAxMtVVROLVVTRVJG
//  aXJzdC1DbGllbnQgQXV0aGVudGljYXRpb24gYW5kIEVtYWlsAhAj50QEWUPwA+jXOiL8sMwXMA0G
//  CSqGSIb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