private RSAPrivateKey loadPrivateKeyFromPEMFile ( String filename ) { try { String publicKeyFile = FileReader . readFileFromClassPathOrPath ( filename ) ; byte [ ] publicKeyBytes = DatatypeConverter . parseBase64Binary ( publicKeyFile . replace ( "-----BEGIN RSA PRIVATE KEY-----" , "" ) . replace ( "-----END RSA PRIVATE KEY-----" , "" ) ) ; return ( RSAPrivateKey ) KeyFactory . getInstance ( "RSA" ) . generatePrivate ( new PKCS8EncodedKeySpec ( publicKeyBytes ) ) ; } catch ( Exception e ) { throw new RuntimeException ( "Exception reading private key from PEM file" , e ) ; } }
Load PrivateKey from PEM file .