BAEL-4219 - How to read .pem file to get private and public key (#9676)

This commit is contained in:
Catalin Burcea
2020-07-12 21:03:46 +03:00
committed by GitHub
parent 51252a02f6
commit 6acb954c1b
8 changed files with 267 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
package com.baeldung.pem;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class JavaSecurityPemUtilsUnitTest {
@Test
public void whenReadPublicKeyFromPEMFile_thenSuccess() throws Exception {
File pemFile = new File(JavaSecurityPemUtilsUnitTest.class.getResource("/pem/public-key.pem").getFile());
RSAPublicKey publicKey = JavaSecurityPemUtils.readX509PublicKey(pemFile);
assertEquals("X.509", publicKey.getFormat());
assertEquals("RSA", publicKey.getAlgorithm());
}
@Test
public void whenReadPrivateKeyFromPEMFile_thenSuccess() throws Exception {
File pemFile = new File(JavaSecurityPemUtilsUnitTest.class.getResource("/pem/private-key-pkcs8.pem").getFile());
RSAPrivateKey privateKey = JavaSecurityPemUtils.readPKCS8PrivateKey(pemFile);
assertEquals("PKCS#8", privateKey.getFormat());
assertEquals("RSA", privateKey.getAlgorithm());
}
}