Sample code for 30+ languages & platforms
Java

Duplicate openssl smime -encrypt -binary -aes-256-cbc -in some_file.dat -out some_file.dat.enc -outform DER cert.crt

See more OpenSSL Examples

Demonstrates how to encrypt to binary DER using 256-bit AES (CBC mode) as the underlying symmetric encryption algorithm, to produce PKCS7 enveloped data (binary DER).

Duplicates the following openssl command:

openssl smime -encrypt -binary -aes-256-cbc -in some_file.dat -out some_file.dat.enc -outform DER cert.crt

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
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 requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Cert cert = new Cert();
        try {
            cert.loadFromFile("qa_data/openssl/EE.cer");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        BinData bd = new BinData();
        bd.loadFile("qa_data/openssl/hello.txt");
        // Assuming success..

        Crypt2 crypt = new Crypt2();
        try {
            crypt.setEncryptCert(cert);

            crypt.setCryptAlgorithm("PKI");

            // Indicate the underlying symmetric encryption to be used:
            crypt.setPkcs7CryptAlg("aes");
            crypt.setKeyLength(256);
            crypt.setCipherMode("cbc");

            crypt.ckEncryptFile("qa_data/openssl/hello.txt", "qa_output/hello.txt.enc");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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