Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:01:00+00:00 2026-05-24T23:01:00+00:00

I need to sign XML SOAP requests in Android application. I did a little

  • 0

I need to sign XML SOAP requests in Android application.

I did a little research and it seems that there is no framework that can do that on Android.

Has anyone managed to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:01:01+00:00Added an answer on May 24, 2026 at 11:01 pm

    After weeks of trying and testing finally I have managed to do manual XML signing on android.
    Here is the code:

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    
    import javax.crypto.Cipher;
    
    import android.util.Log;
    
    /**
     * Generates Sha1withRSA XML signatures.
     */
    public final class XmlSigner {
    
      /** Log tag. */
      private static final String LOG_TAG = "XmlSigner";
    
      /**
       * DER encoded ASN.1 identifier for SHA1 digest: "1.3.14.3.2.26".
       */
      private static final byte[] DER_SHA1_DIGEST_IDENTIFIER = new byte[]{48, 33, 48, 9, 6, 5, 43, 14,
          3, 2, 26, 5, 0, 4, 20};
    
      /** The characters needed for base 64 encoding. */
      private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
    
      /**
       * Hide constructor.
       */
      private XmlSigner() {
    
      }
    
      /**
       * Generates Sha1withRSA XML signature for the specified XML content and
       * private key.
       * 
       * @param xml the XML content
       * @param privateKey the private key to generate the signature
       * @return the whole signature node XML string that should be inserted
       * somewhere in the XML
       * @throws XmlSignerException if the signature XML can not be generated
       */
      public static String generateXmlSignature(String xml, PrivateKey privateKey) throws Throwable {
    
        try {
          // get canonized XML
          int soapBodyStart = xml.indexOf("<soap:Body>");
          int soapBodyEnd = xml.indexOf("</soap:Body>");
          String bodyXml = xml.substring(soapBodyStart + 12, soapBodyEnd - 1);
    
          // get bytes from the XML
          byte[] xmlBytes = bodyXml.getBytes("UTF-8");
    
          // calculate SHA256 digest from the XML bytes
          byte[] xmlDigestBytes = sha1Digest(xmlBytes);
    
          // encode the XML digest to base64 string
          String base64XmlDigest = base64encode(xmlDigestBytes, false);
    
          // generate SignedInfo node to be signed with signature
          String signedInfo = "<ds:SignedInfo xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">"
              + "<ds:CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\">"
              + "</ds:CanonicalizationMethod><ds:SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\">"
              + "</ds:SignatureMethod><ds:Reference URI=\"\"><ds:Transforms>"
              + "<ds:Transform Algorithm=\"http://www.w3.org/2002/06/xmldsig-filter2\">"
              + "<ds:XPath xmlns:ds=\"http://www.w3.org/2002/06/xmldsig-filter2\" Filter=\"intersect\">"
              + "/soap:Envelope/soap:Body/*</ds:XPath></ds:Transform>"
              + "<ds:Transform xmlns:ds=\"http://www.w3.org/2002/06/xmldsig-filter2\" "
              + "Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"></ds:Transform>"
              + "</ds:Transforms><ds:DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\">"
              + "</ds:DigestMethod><ds:DigestValue>" + base64XmlDigest
              + "</ds:DigestValue></ds:Reference></ds:SignedInfo>";
    
          // get the bytes from SignedInfo that will be signed
          byte[] signedInfoBytes = signedInfo.getBytes("UTF-8");
    
          // calculate SHA1 digest of the signed info bytes
          byte[] signedInfoSha1Digest = sha1Digest(signedInfoBytes);
    
          // encode the digest identifier and the SHA1 digest in DER format
          byte[] signedInfoDerSha1Digest = mergeArrays(DER_SHA1_DIGEST_IDENTIFIER, signedInfoSha1Digest);
    
          // initialize RSA cipher with the parameters from the private key
          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
          cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    
          // encrypt the DER encoded SHA1 digest of signed info
          byte[] signatureBytes = cipher.doFinal(signedInfoDerSha1Digest);
    
          // encode the signature bytes into base64 string
          String base64RsaSignature = base64encode(signatureBytes, true);
    
          // generate the whole signature node XML string
          String signature = "<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis"
              + "-200401-wss-wssecurity-secext-1.0.xsd\">"
              + "<ds:Signature xmlns:ds=\"http://www.w3.org/2000/09/xmldsig#\">" + signedInfo
              + "<ds:SignatureValue>" + base64RsaSignature
              + "</ds:SignatureValue></ds:Signature></wsse:Security>";
    
          return signature;
        } catch (Throwable e) {
          Log.e(LOG_TAG, "Error generating signature for XML", e);
          throw e;
        }
      }
    
      /**
       * Merges two byte arrays in one.
       * 
       * @param array1 the first array
       * @param array2 the second array
       * @return merged array
       */
      public static byte[] mergeArrays(byte[] array1, byte[] array2) {
        byte[] temp = new byte[array1.length + array2.length];
        System.arraycopy(array1, 0, temp, 0, array1.length);
        System.arraycopy(array2, 0, temp, array1.length, array2.length);
        return temp;
      }
    
      /**
       * Generates SHA-1 digest of the provided data.
       * 
       * @param data the data to digest
       * @return SHA-1 digest of the provided data.
       */
      public static byte[] sha1Digest(byte[] data) {
        MessageDigest mdSha1 = null;
        try {
          mdSha1 = MessageDigest.getInstance("SHA-1");
        } catch (NoSuchAlgorithmException e1) {
          Log.e(LOG_TAG, "Error initializing SHA1 message digest");
        }
        mdSha1.update(data);
        byte[] sha1hash = mdSha1.digest();
        return sha1hash;
      }
    
    
      /**
       * Generates SHA-256 digest of the provided data.
       * 
       * @param data the data to digest
       * @return SHA-256 digest of the provided data.
       */
      public static byte[] sha256Digest(byte[] data) {
        MessageDigest mdSha256 = null;
        try {
          mdSha256 = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e1) {
          Log.e(LOG_TAG, "Error initializing SHA1 message digest");
        }
        mdSha256.update(data);
        byte[] sha256hash = mdSha256.digest();
        return sha256hash;
      }
    
      /**
       * Encoded byte arrays into Base64 strings.
       * 
       * @param data the byte array to encode
       * @param wrapLines <code>true</code> to add \r\n
       * @return base64 encoded string
       */
      public static String base64encode(byte[] data, boolean wrapLines) {
        int length = data.length;
        StringBuilder sb = new StringBuilder(length * 3 / 2);
        int end = length - 3;
        int i = 0;
        int n = 0;
    
        while (i <= end) {
          int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 0x0ff) << 8)
              | (((int) data[i + 2]) & 0x0ff);
    
          sb.append(CHARS[(d >> 18) & 63]);
          sb.append(CHARS[(d >> 12) & 63]);
          sb.append(CHARS[(d >> 6) & 63]);
          sb.append(CHARS[d & 63]);
    
          i += 3;
    
          if (n++ >= 14) {
            n = 0;
            if (wrapLines) {
              sb.append("\r\n");
            }
          }
        }
    
        if (i == length - 2) {
          int d = ((((int) data[i]) & 0x0ff) << 16) | ((((int) data[i + 1]) & 255) << 8);
          sb.append(CHARS[(d >> 18) & 63]);
          sb.append(CHARS[(d >> 12) & 63]);
          sb.append(CHARS[(d >> 6) & 63]);
          sb.append("=");
        } else if (i == length - 1) {
          int d = (((int) data[i]) & 0x0ff) << 16;
    
          sb.append(CHARS[(d >> 18) & 63]);
          sb.append(CHARS[(d >> 12) & 63]);
          sb.append("==");
        }
    
        return sb.toString();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# application that uses XML digital signatures to sign license files.
I need to sign Android application ( .apk ). I have .pfx file. I
I need to sign my soap request to a 3rd party. The provided an
We need to sign a SOAP message with a certificate in Websphere 7. Currently
I need, in C# (4.0), to sign an XML (XMLDSig Envelope) using a X509Certificate
Why should I sign my JAR files? I know that I need to sign
I'm using an XML POST to sign in my users and I need to
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
I am aware of the Amazon.com exposed URIs ... which I need to sign
I need to make a replace of a plus sign in a javascript string.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.