Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. public static Key importRsaKey(String pathname, KeyType type) throws IOException {
  2.     try {
  3.         byte[] readBytes = Files.readAllBytes(Paths.get(pathname));
  4.         switch (type) {
  5.             case PRIVATE:
  6.                 return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(readBytes));
  7.             case PUBLIC:
  8.                 return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(readBytes));
  9.         }
  10.     } catch (InvalidKeySpecException e) {
  11.         System.out.println("Invalid key file");
  12.         e.printStackTrace();
  13.     } catch (NoSuchAlgorithmException e) {
  14.         //Should not happen.
  15.         e.printStackTrace();
  16.     }
  17.     return null;
  18. }
  19.  
  20. private enum KeyType {
  21.     PRIVATE, PUBLIC
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement