Sample code for 30+ languages & platforms
Java

Use Base64 RSA Key to Encrypt

See more RSA Examples

Loads a Base64 RSA key and uses it to encrypt a string, returning the result in base64.

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;

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

    CkPublicKey pubkey = new CkPublicKey();

    success = pubkey.LoadBase64("MIICdgIBADA ... A9PXLk+j5A==");
    if (success == false) {
        System.out.println(pubkey.lastErrorText());
        return;
        }

    CkRsa rsa = new CkRsa();

    success = rsa.UsePublicKey(pubkey);
    if (success == false) {
        System.out.println(rsa.lastErrorText());
        return;
        }

    rsa.put_EncodingMode("base64");

    String encryptedStr = rsa.encryptStringENC("12345678",false);

    System.out.println(encryptedStr);
  }
}