private KeyPair loadKeyPair ( ) throws IOException , NoSuchAlgorithmException , InvalidKeySpecException { // Read Private Key final File filePrivateKey = getKeyPath ( KeyType . PRIVATE ) ; // Read Public Key final File filePublicKey = getKeyPath ( KeyType . PUBLIC ) ; byte [ ] encodedPrivateKey ; byte [ ] encodedPublicKey ; try ( InputStream pvtfis = Files . newInputStream ( filePrivateKey . toPath ( ) ) ; InputStream pubfis = Files . newInputStream ( filePublicKey . toPath ( ) ) ) { encodedPrivateKey = new byte [ ( int ) filePrivateKey . length ( ) ] ; pvtfis . read ( encodedPrivateKey ) ; encodedPublicKey = new byte [ ( int ) filePublicKey . length ( ) ] ; pubfis . read ( encodedPublicKey ) ; } // Generate KeyPair final KeyFactory keyFactory = KeyFactory . getInstance ( "RSA" ) ; final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec ( encodedPublicKey ) ; final PublicKey publicKey = keyFactory . generatePublic ( publicKeySpec ) ; final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec ( encodedPrivateKey ) ; final PrivateKey privateKey = keyFactory . generatePrivate ( privateKeySpec ) ; return this . keyPair = new KeyPair ( publicKey , privateKey ) ; }
Loads a key pair .