Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkCertW.h>
#include <C_CkCrypt2W.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xml;
    const wchar_t *Body;
    HCkCertW cert;
    HCkCrypt2W crypt;
    HCkBinDataW bd;

    success = FALSE;

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

    xml = CkXmlW_Create();
    success = CkXmlW_LoadXmlFile(xml,L"qa_data/xml/govTalkMessageResponse.xml");
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlW_lastErrorText(xml));
        CkXmlW_Dispose(xml);
        return;
    }

    Body = CkXmlW_getChildContent(xml,L"Body|Message|Body");
    wprintf(L"%s\n",Body);

    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/govTalkMessage_aaa.pfx",L"aaa");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        return;
    }

    crypt = CkCrypt2W_Create();
    CkCrypt2W_putCryptAlgorithm(crypt,L"pki");
    success = CkCrypt2W_SetDecryptCert(crypt,cert);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkCrypt2W_Dispose(crypt);
        return;
    }

    bd = CkBinDataW_Create();
    // Append the bytes to bd.
    success = CkBinDataW_AppendEncoded(bd,Body,L"base64");

    // Decrypt in-place.
    success = CkCrypt2W_DecryptBd(crypt,bd);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkXmlW_Dispose(xml);
        CkCertW_Dispose(cert);
        CkCrypt2W_Dispose(crypt);
        CkBinDataW_Dispose(bd);
        return;
    }

    // Save the decrypted data to a file.
    success = CkBinDataW_WriteFile(bd,L"qa_output/out.dat");

    // If the decrypted data is non-text (binary) then we can examine it in an encoding, such as hex:
    wprintf(L"Decrypted bytes as hex: %s\n",CkBinDataW_getEncoded(bd,L"hex"));


    CkXmlW_Dispose(xml);
    CkCertW_Dispose(cert);
    CkCrypt2W_Dispose(crypt);
    CkBinDataW_Dispose(bd);

    }