Sample code for 30+ languages & platforms
Java

CAdES BES Detached Signature

See more Encryption Examples

Demonstrates how to create a CAdES BES detached signature file (.p7s).

Chilkat Java Downloads

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

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();

        // Use a digital certificate and private key from a PFX file (.pfx or .p12).
        String pfxPath = "/Users/chilkat/testData/pfx/acme.pfx";
        String pfxPassword = "test123";

        Cert cert = new Cert();
        try {
            cert.loadPfxFile(pfxPath, pfxPassword);

            // Tell the crypt component to use this cert.
            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);

        // We can sign any type of file, creating a .p7s as output:
        String inFile = "/Users/chilkat/testData/pdf/sample.pdf";
        String sigFile = "/Users/chilkat/testData/p7s/sample.p7s";

        // Create the detached CAdES-BES signature:
        try {
            crypt.createP7S(inFile, sigFile);

            crypt.verifyP7S(inFile, sigFile);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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