Skip to content

Instantly share code, notes, and snippets.

@gregorej
Last active August 29, 2015 14:17
Show Gist options
  • Save gregorej/2a3b7c1b417d59c140af to your computer and use it in GitHub Desktop.
Save gregorej/2a3b7c1b417d59c140af to your computer and use it in GitHub Desktop.
Get public key from X509 certificate
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import org.junit.Test;
public class CertificatesTest {
@Test
public void getPublickeyFromCertificate() throws CertificateException, FileNotFoundException {
final String certPath = "from_rest.cert";
FileInputStream fin = new FileInputStream(certPath);
CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)f.generateCertificate(fin);
PublicKey pk = certificate.getPublicKey();
System.out.println(pk.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment