그 외
[그 외] public key, private key file로부터 가져오기
Dong's Universe
2024. 5. 3. 22:53
private PublicKey getPublicKey() {
File publicKeyFile = new File("src/main/resources/public.key");
PublicKey publicKey = null;
try {
byte[] publicKeyBytes = Files.readAllBytes(publicKeyFile.toPath());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
publicKey = keyFactory.generatePublic(publicKeySpec);
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
log.info("an error occured obtaining key");
log.info(e.getMessage());
}
return publicKey;
}
private PrivateKey getPrivateKey() {
File privateKeyFile = new File("src/main/resources/private.key");
PrivateKey privateKey = null;
try {
byte[] privateKeyBytes = Files.readAllBytes(privateKeyFile.toPath());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
log.info("an error occured obtaining key");
log.info(e.getMessage());
}
return privateKey;
}