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

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

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;

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.

        PublicKey pubkey = new PublicKey();

        try {
            pubkey.loadBase64("MIICdgIBADA ... A9PXLk+j5A==");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rsa rsa = new Rsa();

        try {
            rsa.usePublicKey(pubkey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        rsa.setEncodingMode("base64");

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

        System.out.println(encryptedStr);
    }
}