Sample code for 30+ languages & platforms
Java

Create XAdES using Smart Card or USB Token

See more XAdES Examples

Demonstrates how to create an XAdES signed XML document using a certificate located on a smartcard or USB token.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
import com.chilkatsoft.chilkat2.XmlDSig;
import com.chilkatsoft.chilkat2.XmlDSigGen;

public class ChilkatExample {

    public static void main(String[] args) {
        // Load the XML to be signed.
        Xml xmlToSign = new Xml();
        try {
            xmlToSign.loadXmlFile("qa_data/fattura_electronica/docToSign.xml");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        XmlDSigGen gen = new XmlDSigGen();

        gen.setSigLocation("p:FatturaElettronica");
        gen.setSigId("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
        gen.setSigNamespacePrefix("ds");
        gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
        gen.setSigValueId("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-sigvalue");
        gen.setSignedInfoCanonAlg("C14N");
        gen.setSignedInfoDigestMethod("sha256");

        // Create an Object to be added to the Signature.
        // Note: Chilkat will automatically populate the strings indicated by "TO BE GENERATED BY CHILKAT" with actual/correct values
        // when the XML is signed.
        Xml object1 = new Xml();
        object1.setTag("xades:QualifyingProperties");
        object1.addAttribute("xmlns:xades", "http://uri.etsi.org/01903/v1.3.2#");
        object1.addAttribute("xmlns:xades141", "http://uri.etsi.org/01903/v1.4.1#");
        object1.addAttribute("Target", "#xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504");
        object1.updateAttrAt("xades:SignedProperties", true, "Id", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops");
        object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningTime", "TO BE GENERATED BY CHILKAT");
        object1.updateAttrAt("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestMethod", true, "Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
        object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:CertDigest|ds:DigestValue", "TO BE GENERATED BY CHILKAT");
        object1.updateChildContent("xades:SignedProperties|xades:SignedSignatureProperties|xades:SigningCertificateV2|xades:Cert|xades:IssuerSerialV2", "TO BE GENERATED BY CHILKAT");

        gen.addObject("", object1.getXml(), "", "");

        // -------- Reference 1 --------
        gen.setKeyInfoId("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo");
        gen.addSameDocRef("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-keyinfo", "sha256", "", "", "");

        // -------- Reference 2 --------
        gen.addSameDocRef("", "sha256", "", "", "");
        gen.setRefIdAttr("", "xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-ref0");

        // -------- Reference 3 --------
        gen.addObjectRef("xmldsig-6f4b994a-7191-4bb1-ab3c-17549515b504-signedprops", "sha256", "", "", "http://uri.etsi.org/01903#SignedProperties");

        // ----------------------------------------------------------------
        //  Load a certificate that has been pre-installed on the Windows system
        //  This includes certificates on smartcards and USB tokens
        Cert cert = new Cert();

        //  You may provide the PIN here..
        cert.setSmartCardPin("000000");

        // Load the certificate on the smartcard currently in the reader (or on the USB token).
        // Pass an empty string to allow Chilkat to automatically choose the CSP (Cryptographi Service Provider).
        // See Load Certificate on Smartcard for information about explicitly selecting a particular CSP.
        try {
            cert.loadFromSmartcard("");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        gen.setX509Cert(cert, true);
        gen.setKeyInfoType("X509Data");
        gen.setX509Type("Certificate");

        // Load XML to be signed...
        StringBuilder sbXml = new StringBuilder();
        xmlToSign.getXmlSb(sbXml);

        gen.setBehaviors("IndentedSignature,ForceAddEnvelopedSignatureTransform");

        // Sign the XML...
        try {
            gen.createXmlDSigSb(sbXml);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Save the signed XMl to a file.
        sbXml.writeFile("qa_output/signedXml.xml", "utf-8", false);

        System.out.println(sbXml.getAsString());

        // ----------------------------------------
        // Verify the signature we just produced...
        XmlDSig verifier = new XmlDSig();
        try {
            verifier.loadSignatureSb(sbXml);

            verifier.verifySignature(true);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("This signature was successfully verified.");
    }
}