Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

RSA Encrypt and OpenSSL Decrypt

See more OpenSSL Examples

Demonstrates how to use Chilkat to RSA encrypt, and then use OpenSSL to decrypt.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.PrivateKey;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;

public class ChilkatExample {

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

        Rsa rsa = new Rsa();

        PrivateKey privKey = new PrivateKey();
        rsa.genKey(2048, privKey);
        privKey.savePkcs8PemFile("qa_output/privKey.pem");

        PublicKey pubKey = new PublicKey();
        privKey.toPublicKey(pubKey);

        rsa.setEncodingMode("base64");
        String plainText = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890";
        boolean bUsePrivateKey = false;
        rsa.usePublicKey(pubKey);
        String encryptedStr = rsa.encryptStringENC(plainText, bUsePrivateKey);

        BinData bd = new BinData();
        bd.appendEncoded(encryptedStr, "base64");
        bd.writeFile("qa_output/enc.dat");

        // The OpenSSL command to decrypt is:
        // openssl pkeyutl -in enc.dat -inkey privKey.pem -keyform PEM -pkeyopt rsa_padding_mode:pkcs1 -decrypt

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