Sample code for 30+ languages & platforms
Java

RSA Import Public Key from Certificate PEM

See more RSA Examples

Uses a certificate's public key for RSA encryption. The public key from the certificate .pem file is used.

Chilkat Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    boolean success = false;

    CkCert cert = new CkCert();

    success = cert.LoadFromFile("qa_data/pem/mf_public_rsa.pem");
    if (success == false) {
        System.out.println(cert.lastErrorText());
        return;
        }

    CkPublicKey pubKey = new CkPublicKey();
    cert.GetPublicKey(pubKey);

    CkRsa rsa = new CkRsa();
    rsa.UsePublicKey(pubKey);

    rsa.put_EncodingMode("base64");
    String encryptedStr = rsa.encryptStringENC("hello",false);
    System.out.println("encrypted string = " + encryptedStr);
  }
}