Sample code for 30+ languages & platforms
PowerBuilder

Decrypt a govtalk.gov.uk SOAP GovTalkMessage

See more Encryption Examples

Demonstrates how to decrypt the content contained in the XML of a GovTalkMessage SOAP response.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Xml
string ls_Body
oleobject loo_Cert
oleobject loo_Crypt
oleobject loo_Bd

li_Success = 0

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

// The GovTalkMessage response looks something like this:

// <?xml version="1.0" encoding="utf-8"?>
// <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope">
//   <EnvelopeVersion>3.1</EnvelopeVersion>
//   <Header>
//     <MessageDetails>
//       <Class>CSSZ_DZDPN</Class>
//       <Qualifier>request</Qualifier>
//       <Function>submit</Function>
//       <TransactionID />
//       <AuditID />
//       <CorrelationID>aaaaa</CorrelationID>
//       <ResponseEndPoint PollInterval="0" />
//       <Transformation>XML</Transformation>
//       <GatewayTest />
//       <GatewayTimestamp />
//     </MessageDetails>
//     <SenderDetails>
//       <IDAuthentication>
//         <SenderID />
//         <Authentication>
//           <Method>clear</Method>
//           <Role />
//           <Value />
//         </Authentication>
//       </IDAuthentication>
//       <X509Certificate />
//       <EmailAddress>somebody@example.com</EmailAddress>
//     </SenderDetails>
//   </Header>
//   <GovTalkDetails>
//     <Keys>
//       <Key Type="vars">9999999999</Key>
//     </Keys>
//     <GatewayAdditions>
//       <Source>VREP</Source>
//     </GatewayAdditions>
//   </GovTalkDetails>
//   <Body>
//     <Message xmlns="http://www.cssz.cz/XMLSchema/envelope" version="1.2" eType="DZDPN20">
//       <Header>
//         <Signature xmlns:dt="urn:schemas-microsoft-com:datatypes" dt:dt="bin.base64">MIIJ0A ... UMw=
// </Signature>
//         <Vendor productName="some product name" version="2019" />
//       </Header>
//       <Body xmlns:dt="urn:schemas-microsoft-com:datatypes" encrypted="yes" contentEncoding="gzip" dt:dt="bin.base64">MIIF2w ... N2vW</Body>
//     </Message>
//   </Body>
// </GovTalkMessage>

// We want to get the content of the Body and decrypt it.

// First, let's get the content of the Body XML element, which is a base64 string starting with MIIF2w...

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Xml.LoadXmlFile("qa_data/xml/govTalkMessageResponse.xml")
if li_Success = 0 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Xml
    return
end if

ls_Body = loo_Xml.GetChildContent("Body|Message|Body")
Write-Debug ls_Body

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

li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/govTalkMessage_aaa.pfx","aaa")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Xml
    destroy loo_Cert
    return
end if

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Crypt.CryptAlgorithm = "pki"
li_Success = loo_Crypt.SetDecryptCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Xml
    destroy loo_Cert
    destroy loo_Crypt
    return
end if

loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

// Append the bytes to bd.
li_Success = loo_Bd.AppendEncoded(ls_Body,"base64")

// Decrypt in-place.
li_Success = loo_Crypt.DecryptBd(loo_Bd)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Xml
    destroy loo_Cert
    destroy loo_Crypt
    destroy loo_Bd
    return
end if

// Save the decrypted data to a file.
li_Success = loo_Bd.WriteFile("qa_output/out.dat")

// If the decrypted data is non-text (binary) then we can examine it in an encoding, such as hex:
Write-Debug "Decrypted bytes as hex: " + loo_Bd.GetEncoded("hex")


destroy loo_Xml
destroy loo_Cert
destroy loo_Crypt
destroy loo_Bd