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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

Chilkat Java Downloads

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

        Rsa rsa = new Rsa();

        // First demonstrate importing a PEM public key:
        String publicKeyPem = "PEM public-key data goes here";
        PublicKey pubkey = new PublicKey();

        try {
            pubkey.loadFromString(publicKeyPem);

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

        // Demonstrate importing a PEM private key:
        String privateKeyPem = "PEM private-key data goes here";
        PrivateKey privkey = new PrivateKey();

        // If the private key PEM is protected with a password, then
        // call LoadEncryptedPem.  Otherwise call LoadPem.
        try {
            privkey.loadPem(privateKeyPem);

            rsa.usePrivateKey(privkey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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