Skip to content

Instantly share code, notes, and snippets.

@Somsubhra
Created March 19, 2023 10:13
Show Gist options
  • Select an option

  • Save Somsubhra/8b0d6c3aefc1528ca5b6dcfbf921145e to your computer and use it in GitHub Desktop.

Select an option

Save Somsubhra/8b0d6c3aefc1528ca5b6dcfbf921145e to your computer and use it in GitHub Desktop.
Utilities to manage RSA Keys in Java
package org.evernet.common.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
@Component
public class KeyUtils {
private final String nodeIdentifier;
@Autowired
public KeyUtils(@Value("${evernet.node.identifier}") String nodeIdentifier) {
this.nodeIdentifier = nodeIdentifier;
}
public KeyPair getKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
Path directoryPath = Path.of(System.getProperty("user.home"), ".evernet", "keys");
Files.createDirectories(directoryPath);
Path privateKeyPath = Path.of(directoryPath.toString(), nodeIdentifier + ".private.key");
Path publicKeyPath = Path.of(directoryPath.toString(), nodeIdentifier + ".public.key");
if (Files.exists(privateKeyPath)) {
byte[] bytes = Files.readAllBytes(privateKeyPath);
PrivateKey privateKey = convertBytesToPrivateKey(bytes);
bytes = Files.readAllBytes(publicKeyPath);
PublicKey publicKey = convertBytesToPublicKey(bytes);
return new KeyPair(publicKey, privateKey);
} else {
KeyPair pair = generateKeyPair();
try (FileOutputStream fos = new FileOutputStream(publicKeyPath.toFile())) {
fos.write(convertPublicKeyToBytes(pair.getPublic()));
}
try (FileOutputStream fos = new FileOutputStream(privateKeyPath.toFile())) {
fos.write(convertPrivateKeyToBytes(pair.getPrivate()));
}
return pair;
}
}
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(4096);
return generator.generateKeyPair();
}
public static PublicKey convertBytesToPublicKey(byte[] publicKeyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
return KeyFactory.getInstance("RSA").generatePublic(keySpec);
}
public static PublicKey convertBase64StringToPublicKey(String publicKeyString) throws InvalidKeySpecException, NoSuchAlgorithmException {
byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
return convertBytesToPublicKey(publicKeyBytes);
}
public static PrivateKey convertBytesToPrivateKey(byte[] privateKeyBytes) throws NoSuchAlgorithmException, InvalidKeySpecException {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(keySpec);
}
public static PrivateKey convertBase64StringToPrivateKey(String privateKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyString);
return convertBytesToPrivateKey(privateKeyBytes);
}
public static byte[] convertPrivateKeyToBytes(PrivateKey privateKey) {
return privateKey.getEncoded();
}
public static String convertPrivateKeyToBase64String(PrivateKey privateKey) {
return Base64.getEncoder().encodeToString(convertPrivateKeyToBytes(privateKey));
}
public static byte[] convertPublicKeyToBytes(PublicKey publicKey) {
return publicKey.getEncoded();
}
public static String convertPublicKeyToBase64String(PublicKey publicKey) {
return Base64.getEncoder().encodeToString(convertPublicKeyToBytes(publicKey));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment