@Test public void file2PublicKey() throws FileNotFoundException { PublicKey publicKey = Converter.File2PublicKey(new FileInputStream("/Users/robert/test.pub"), "RSA"); System.out.println(publicKey); }
public static PublicKey File2PublicKey(FileInputStream stream, String algorithm) { PEMParser pemParser = new PEMParser(new InputStreamReader(stream)); try { PemObject pemObject = pemParser.readPemObject(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pemObject.getContent()); KeyFactory keyFactory = KeyFactory.getInstance(algorithm, new BouncyCastleProvider()); pemParser.close(); return keyFactory.generatePublic(x509EncodedKeySpec); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } finally { try { pemParser.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
Converter { public static PublicKey File2PublicKey(FileInputStream stream, String algorithm) { PEMParser pemParser = new PEMParser(new InputStreamReader(stream)); try { PemObject pemObject = pemParser.readPemObject(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pemObject.getContent()); KeyFactory keyFactory = KeyFactory.getInstance(algorithm, new BouncyCastleProvider()); pemParser.close(); return keyFactory.generatePublic(x509EncodedKeySpec); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } finally { try { pemParser.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
Converter { public static PublicKey File2PublicKey(FileInputStream stream, String algorithm) { PEMParser pemParser = new PEMParser(new InputStreamReader(stream)); try { PemObject pemObject = pemParser.readPemObject(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pemObject.getContent()); KeyFactory keyFactory = KeyFactory.getInstance(algorithm, new BouncyCastleProvider()); pemParser.close(); return keyFactory.generatePublic(x509EncodedKeySpec); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } finally { try { pemParser.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }  }
Converter { public static PublicKey File2PublicKey(FileInputStream stream, String algorithm) { PEMParser pemParser = new PEMParser(new InputStreamReader(stream)); try { PemObject pemObject = pemParser.readPemObject(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pemObject.getContent()); KeyFactory keyFactory = KeyFactory.getInstance(algorithm, new BouncyCastleProvider()); pemParser.close(); return keyFactory.generatePublic(x509EncodedKeySpec); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } finally { try { pemParser.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }  static void cert2File(X509Certificate x509Certificate, FileOutputStream fileOutputStream); static X509Certificate file2Cert(FileInputStream stream); static void PublicKey2File(FileOutputStream stream, PublicKey publicKey); static PublicKey File2PublicKey(FileInputStream stream, String algorithm); static void PrivateKey2File(FileOutputStream stream, PrivateKey privateKey); static PrivateKey File2PrivateKey(FileInputStream stream, String algorithm); static PKCS10CertificationRequest File2PKCS10CertificateRequest(FileInputStream stream); static void PKCS10CertificationRequest2File(PKCS10CertificationRequest request, FileOutputStream stream); static PrivateKey EncryptionPrivateKeyFile2PrivateKey(FileInputStream stream, String algorithm,
                                                                 String password); static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey); static SecretKey SecureKeyFile2SecureKey(FileInputStream stream, String algorithm, Provider provider); static void SecretKey2File(SecretKey secretKey, FileOutputStream fileOutputStream); static SecretKey String2SecureKey(String var1, String algorithm, Provider provider); static String PublicKey2String(PublicKey publicKey); static String PrivateKey2String(PrivateKey privateKey); static PublicKey String2PublicKey(String var, String algorithm); static PrivateKey String2PrivateKey(String var, String algorithm); }
Converter { public static PublicKey File2PublicKey(FileInputStream stream, String algorithm) { PEMParser pemParser = new PEMParser(new InputStreamReader(stream)); try { PemObject pemObject = pemParser.readPemObject(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pemObject.getContent()); KeyFactory keyFactory = KeyFactory.getInstance(algorithm, new BouncyCastleProvider()); pemParser.close(); return keyFactory.generatePublic(x509EncodedKeySpec); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } finally { try { pemParser.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }  static void cert2File(X509Certificate x509Certificate, FileOutputStream fileOutputStream); static X509Certificate file2Cert(FileInputStream stream); static void PublicKey2File(FileOutputStream stream, PublicKey publicKey); static PublicKey File2PublicKey(FileInputStream stream, String algorithm); static void PrivateKey2File(FileOutputStream stream, PrivateKey privateKey); static PrivateKey File2PrivateKey(FileInputStream stream, String algorithm); static PKCS10CertificationRequest File2PKCS10CertificateRequest(FileInputStream stream); static void PKCS10CertificationRequest2File(PKCS10CertificationRequest request, FileOutputStream stream); static PrivateKey EncryptionPrivateKeyFile2PrivateKey(FileInputStream stream, String algorithm,
                                                                 String password); static void PrivateKey2EncryptionFile(FileOutputStream stream, String password, PrivateKey privateKey); static SecretKey SecureKeyFile2SecureKey(FileInputStream stream, String algorithm, Provider provider); static void SecretKey2File(SecretKey secretKey, FileOutputStream fileOutputStream); static SecretKey String2SecureKey(String var1, String algorithm, Provider provider); static String PublicKey2String(PublicKey publicKey); static String PrivateKey2String(PrivateKey privateKey); static PublicKey String2PublicKey(String var, String algorithm); static PrivateKey String2PrivateKey(String var, String algorithm);  }