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

KeyPairgenerateRsaKeyPair(int keysize)
generate Rsa Key Pair
try {
    KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(keysize);
    KeyPair keyPair = generator.generateKeyPair();
    return keyPair;
} catch (Exception e) {
    throw new RuntimeException(e);
KeyStoregenerateRSAKeypairAndKeystore(String fullyQualifiedDN, Date endDate, String keystoreLocation, String keyPairAlias, String keypairPassword, String keystorePassword)
generate RSA Keypair And Keystore
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(2048, random);
KeyPair keyPair = keyGen.generateKeyPair();
X509Certificate cert = generateX509Certificate(fullyQualifiedDN, keyPair, endDate, "SHA1withRSA");
keyStore.setKeyEntry(keyPairAlias, keyPair.getPrivate(), keypairPassword.toCharArray(),
...
RSAPublicKeygenerateRsaPublicKey(BigInteger modulus, BigInteger publicExponent)
generate Rsa Public Key
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return (RSAPublicKey) keyFactory.generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
RSAPublicKeygenerateRSAPublicKey(final BigInteger modulus, final BigInteger publicExponent)
generate RSA Public Key
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, publicExponent);
KeyFactory kf = getKeyFactory("RSA");
return (RSAPublicKey) kf.generatePublic(keySpec);
StringgenerateRSASHA1Signature(PrivateKey privateKey, String data)
Computes RSA-SHA1 signature.
byte[] rsaSha1Data = null;
try {
    Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign(privateKey);
    signature.update(data.getBytes("UTF-8"));
    rsaSha1Data = signature.sign();
    return new BASE64Encoder().encode(rsaSha1Data);
} catch (UnsupportedEncodingException e) {
...
StringgenerateSaltedHMAC(String accessKey, String userSalt, Mac mac)
generate Salted HMAC
byte[] bytes = mac.doFinal((accessKey + ":" + userSalt).getBytes("ASCII"));
return DatatypeConverter.printBase64Binary(bytes);
KeyStoregetPemKeyStore(File file)
get Pem Key Store
KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(null);
if (file != null) {
    byte[] certAndKey = Files.readAllBytes(file.toPath());
    byte[] certBytes = parseDERFromPEM(certAndKey, "-----BEGIN .*CERTIFICATE-----",
            "-----END .*CERTIFICATE-----");
    byte[] pvtKeyBytes = parseDERFromPEM(certAndKey, "-----BEGIN .*PRIVATE KEY-----",
            "-----END .*PRIVATE KEY-----");
...