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

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

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

public class ChilkatExample {

    public static void main(String[] args) {
        Cert cert = new Cert();

        try {
            cert.loadFromFile("qa_data/pem/mf_public_rsa.pem");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        PublicKey pubKey = new PublicKey();
        cert.getPublicKey(pubKey);

        Rsa rsa = new Rsa();
        rsa.usePublicKey(pubKey);

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