Sample code for 30+ languages & platforms
Java

Italian FatturaPA (e-Invoice) Signed XML (CADES-BES P7M) using USB SmartCard Reader

See more CAdES Examples

Demonstrates Italian e-Invoice (FatturaPA) signing by using a private key stored on a USB smartcard reader.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Crypt2 crypt = new Crypt2();
        crypt.setVerboseLogging(true);

        Cert cert = new Cert();
        // Use your smart card user PIN for signing.
        cert.setSmartCardPin("0000");

        try {
            cert.loadFromSmartcard("");

            crypt.setSigningCert(cert);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // The CadesEnabled property applies to all methods that create PKCS7 signatures.
        // To create a CAdES-BES signature, set this property equal to true.
        crypt.setCadesEnabled(true);

        crypt.setHashAlgorithm("sha256");

        JsonObject signedAttrs = new JsonObject();
        signedAttrs.updateInt("contentType", 1);
        signedAttrs.updateInt("signingTime", 1);
        signedAttrs.updateInt("messageDigest", 1);
        signedAttrs.updateInt("signingCertificateV2", 1);
        crypt.setSigningAttributes(signedAttrs.emit());

        // Load XML such as the following:
        //  <p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">
        //     <FatturaElettronicaHeader>
        //        <DatiTrasmissione>
        //     ...
        //        </DatiTrasmissione>
        //        <CedentePrestatore>
        //     ...
        //        </CedentePrestatore>
        //        <CessionarioCommittente>
        //     ...
        //        </CessionarioCommittente>
        //     </FatturaElettronicaHeader>
        //     <FatturaElettronicaBody>
        //        <DatiGenerali>
        //           <DatiGeneraliDocumento>
        //         ...
        //           </DatiGeneraliDocumento>
        //        </DatiGenerali>
        //        <DatiBeniServizi>
        //     ...
        //        </DatiBeniServizi>
        //     </FatturaElettronicaBody>
        //  </p:FatturaElettronica>

        String inputXmlPath = "c:/someDir/e-Invoice.xml";
        String outputP7mPath = "c:/someDir/signed.p7m";

        // Create the CAdES-BES attached signature, which contains the original data.
        try {
            crypt.createP7M(inputXmlPath, outputP7mPath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("Success.");
    }
}