Java Requires Chilkat v11.0.0+
Chilkat2 Java
Generate RSA Public/Private Key Pair and Export to PEM
See more RSA Examples
_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.Chilkat Java Downloads
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();
// Generate a 2048-bit key. Chilkat RSA supports
// key sizes ranging from 512 bits to 8192 bits.
PrivateKey privKey = new PrivateKey();
try {
rsa.genKey(2048, privKey);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
PublicKey pubKey = new PublicKey();
privKey.toPublicKey(pubKey);
// Save the private key in PEM format:
try {
privKey.savePemFile("privateKey.pem");
// Save the public key in PEM format:
pubKey.savePemFile(false, "publicKey.pem");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
}
}