Sample code for 30+ languages & platforms
Android™

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 Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

    CkXml xml = new CkXml();
    success = xml.LoadXmlFile("qa_data/xml/govTalkMessageResponse.xml");
    if (success == false) {
        Log.i(TAG, xml.lastErrorText());
        return;
        }

    String Body = xml.getChildContent("Body|Message|Body");
    Log.i(TAG, Body);

    CkCert cert = new CkCert();
    success = cert.LoadPfxFile("qa_data/pfx/govTalkMessage_aaa.pfx","aaa");
    if (success == false) {
        Log.i(TAG, cert.lastErrorText());
        return;
        }

    CkCrypt2 crypt = new CkCrypt2();
    crypt.put_CryptAlgorithm("pki");
    success = crypt.SetDecryptCert(cert);
    if (success == false) {
        Log.i(TAG, crypt.lastErrorText());
        return;
        }

    CkBinData bd = new CkBinData();
    // Append the bytes to bd.
    success = bd.AppendEncoded(Body,"base64");

    // Decrypt in-place.
    success = crypt.DecryptBd(bd);
    if (success == false) {
        Log.i(TAG, crypt.lastErrorText());
        return;
        }

    // Save the decrypted data to a file.
    success = 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:
    Log.i(TAG, "Decrypted bytes as hex: " + bd.getEncoded("hex"));

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}