Sample code for 30+ languages & platforms
Java

Encrypt using Cert to produce -----BEGIN PKCS7----- ... -----END PKCS7-----

See more Encryption Examples

Demonstrates how to encrypt using a certificate to produce output such as:
-----BEGIN PKCS7-----
MIIHPwYJKoZIhvcNAQcEoIIHMDCCBywC ...
...
...
-----END PKCS7-----
The certificate to be used is not your own, but the certificate of the intended recipient of the message.

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) {
        boolean success = false;

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Crypt2 crypt = new Crypt2();

        // Specify the encryption to be used.
        // "pki" indicates "Public Key Infrastructure" and will create a PKCS7 encrypted (enveloped) message.
        crypt.setCryptAlgorithm("pki");
        crypt.setPkcs7CryptAlg("aes");
        crypt.setKeyLength(128);
        crypt.setOaepHash("sha256");
        crypt.setOaepPadding(true);

        // A certificate is needed as the encryption key..
        Cert cert = new Cert();
        try {
            cert.loadFromFile("qa_data/certs/testCert.pem");
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (!success) {
            System.out.println(cert.getLastErrorText());
            return;
        }

        // Tell the crypt object to use the certificate.
        crypt.setEncryptCert(cert);

        String toBeEncrypted = "This string is to be encrypted.";

        // Get the result in multi-line BASE64 MIME format.
        crypt.setEncodingMode("base64_mime");

        String encryptedStr = crypt.encryptStringENC(toBeEncrypted);
        if (!success) {
            System.out.println(crypt.getLastErrorText());
            return;
        }

        // Make a "-----BEGIN PKCS7-----" ... "-----END PKCS7-----" sandwich...
        StringBuilder sb = new StringBuilder();
        sb.appendLine("-----BEGIN PKCS7-----", true);
        sb.append(encryptedStr);
        sb.appendLine("-----END PKCS7-----", true);

        String outStr = sb.getAsString();

        System.out.println(outStr);

        // Sample output:

        // -----BEGIN PKCS7-----
        // MIICYQYJKoZIhvcNAQcDoIICUjCCAk4CAQAxggH5MIIB9QIBADCBsTCBmzELMAkGA1UEBhMCR0Ix
        // GzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR
        // Q09NT0RPIENBIExpbWl0ZWQxQTA/BgNVBAMTOENPTU9ETyBTSEEtMjU2IENsaWVudCBBdXRoZW50
        // aWNhdGlvbiBhbmQgU2VjdXJlIEVtYWlsIENBAhEAuBl4qE2MODB05C5h53M5UDA4BgkqhkiG9w0B
        // AQcwK6APMA0GCWCGSAFlAwQCAQUAoRgwFgYJKoZIhvcNAQEIMAkGBSsOAwIaBQAEggEAyZejlE37
        // awl0bCWVbOCqf9yLSN17mZRamG8FHDh3nNu11G0+oyJtsPDEnSKsQig0V67MZ+hcWV+uf4ytcjyx
        // H0gs5uex+LwkB+c3ZTOt18IYWFtRilg1HFy1ZN3t0D2QbxYy+i1TXOOwp3gAHL45vRCJ0FbKyQ36
        // pKl0XLe+lRvp2EiJCKVjxtX8VcZOKT4xkG7yOARaCQceth6pA58Dg0yzAz7w4nD2UgAlNzrXG69X
        // e+7e7yfBv47RRqFiQqDpCn+fM/PmFbUyqBppMwc64yP+fJek8VyJw2/UaXWWM4iSKSflk90tiHwf
        // loEU3It4arnSv94fZQo0v129aBqpWzBMBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAECBBCohUgm5qX+
        // TE6PxtPCmWi8gCBgbNg39emAB+AqLozm+vSLjZOGfg3M52gccKUJ8tg8XQ==
        // -----END PKCS7-----
    }
}