Java Utililty Methods RSA

List of utility methods to do RSA

Description

The list of methods to do RSA are organized into topic(s).

Method

MapgenerateKeys()
generate Keys
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair kp = keyGen.genKeyPair();
byte[] publicKey = kp.getPublic().getEncoded();
byte[] privateKey = kp.getPrivate().getEncoded();
Base64.Encoder encoder = Base64.getEncoder();
String publicRSA = "-----BEGIN PUBLIC KEY-----\n" + encoder.encodeToString(privateKey)
        + "\n-----END PUBLIC KEY-----\n";
...
KeyPairgenerateRSA(int keySize)
generate RSA
KeyPairGenerator g = KeyPairGenerator.getInstance("RSA", "BC");
g.initialize(keySize, new SecureRandom());
return g.generateKeyPair();
KeyPairgenerateRSAKey(int keySize)
Generates a new RSA key with specified keySize
try {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(keySize);
    return generator.genKeyPair();
} catch (NoSuchAlgorithmException ex) {
    throw new RuntimeException(ex);
KeyPairgenerateRSAKeyPair()
Create a random 1024 bit RSA key pair
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
kpGen.initialize(1024, new SecureRandom());
return kpGen.generateKeyPair();
KeyPairgenerateRSAKeyPair()
generate RSA Key Pair
try {
    KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("RSA");
    keyGenerator.initialize(1024);
    return keyGenerator.genKeyPair();
} catch (Exception ex) {
    return null;
KeyPairgenerateRsaKeyPair()
Generates a new RSA key pair.
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
KeyPair kp = kg.generateKeyPair();
return kp;
KeyPairgenerateRSAKeyPair()
generate RSA Key Pair
try {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM_KEY);
    kpg.initialize(2048);
    KeyPair keyPair = kpg.genKeyPair();
    return keyPair;
} catch (NoSuchAlgorithmException e) {
    throw new RuntimeException("Unknown encryption algorithm provided: " + RSA_ALGORITHM_KEY, e);
KeyPairgenerateRSAKeyPair(final int bits)
Generate a new asymmetric encryption key pair.
KeyPairGenerator gen = KeyPairGenerator.getInstance(RSA_ALGORITHM);
gen.initialize(bits);
return gen.generateKeyPair();
KeyPairgenerateRSAKeypair(final int keysize, BigInteger publicExponent)
generate RSA Keypair
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA", "BC");
if (publicExponent == null) {
    publicExponent = RSAKeyGenParameterSpec.F4;
AlgorithmParameterSpec params = new RSAKeyGenParameterSpec(keysize, publicExponent);
kpGen.initialize(params);
return kpGen.generateKeyPair();
KeyPairgenerateRSAKeyPair(int bitsize)
generate RSA Key Pair
KeyPair keypair = null;
try {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(bitsize); 
    keypair = kpg.genKeyPair();
} catch (Exception ex) {
    ex.printStackTrace();
return keypair;