Sample code for 30+ languages & platforms
Java

Create PEM-encoded PKCS#7 Detached Signature

See more Digital Signatures Examples

Demonstrates how to create a PKCS7 PEM-encoded object containing a detached signature.

Chilkat Java Downloads

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

public class ChilkatExample {

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

        Cert cert = new Cert();

        // Load the cert and private key.
        try {
            cert.loadPfxFile("qa_data/pfx/myCertAndKey.p12", "password");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Crypt2 crypt = new Crypt2();

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

        // Use SHA-256
        crypt.setHashAlgorithm("sha256");

        // Hash the utf-8 byte representation of the string
        crypt.setCharset("utf-8");

        // Return the result in base64
        crypt.setEncodingMode("base64Mime");

        // Sign some text to create a detached signature (i.e. a signature that does not include the signed data)
        String textToSign = "This is the text to be hashed and signed.";
        String sigBase64;
        try {
            sigBase64 = crypt.signStringENC(textToSign);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println(sigBase64);

        // The result:

        // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
        // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx
        // ...
        // ...

        // If we want it in PEM format with just LF line-endings:
        StringBuilder sb = new StringBuilder();
        // Just LF line endings, not CRLF.
        boolean crlf = false;
        sb.appendLine("-----BEGIN PKCS7-----", crlf);
        sb.append(sigBase64);
        sb.appendLine("-----END PKCS7-----", crlf);
        sb.toLF();

        // Save to a file.
        sb.writeFile("c:/temp/qa_output/sig.pem", "utf-8", false);

        // Examine..
        System.out.println(sb.getAsString());

        // Result is:

        // -----BEGIN PKCS7-----
        // MIIWbgYJKoZIhvcNAQcCoIIWXzCCFlsCAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg
        // ghMXMIIFuzCCA6OgAwIBAgIIVwoRl0LE48wwDQYJKoZIhvcNAQELBQAwazELMAkGA1UEBhMCSVQx
        // DjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3RhbGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUG
        // A1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBSb290IENBMB4XDTExMDkyMjExMjIwMloXDTMw
        // MDkyMjExMjIwMlowazELMAkGA1UEBhMCSVQxDjAMBgNVBAcMBU1pbGFuMSMwIQYDVQQKDBpBY3Rh
        // bGlzIFMucC5BLi8wMzM1ODUyMDk2NzEnMCUGA1UEAwweQWN0YWxpcyBBdXRoZW50aWNhdGlvbiBS
        // b290IENBMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAp8bEpSmkLO/lGMWwUKNvUTuf
        // ClrJwkg4CsIcoBh/kbWHuUA/3R1oHwiD1S0eiKD4j1aPbZkCkpAW1V8IbInX4ay8IMKx4INRimlN
        // AJZaby/ARH6jDuSRzVju3PvHHkVH3Se5CAGfpiEd9UEtL0z9KK3giq0itFZljoZUj5NDKd45Rnij
        // MCO6zfB9E1fAXdKDa0hMxKufgFpbOr3JpyI/gCczWw63igxdBzcIy2zSekciRDXFzMwujt0q7bd9
        // ...
        // ...
        // -----END PKCS7-----
    }
}