Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Key importRsaKey(String pathname, KeyType type) throws IOException {
- try {
- byte[] readBytes = Files.readAllBytes(Paths.get(pathname));
- switch (type) {
- case PRIVATE:
- return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(readBytes));
- case PUBLIC:
- return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(readBytes));
- }
- } catch (InvalidKeySpecException e) {
- System.out.println("Invalid key file");
- e.printStackTrace();
- } catch (NoSuchAlgorithmException e) {
- //Should not happen.
- e.printStackTrace();
- }
- return null;
- }
- private enum KeyType {
- PRIVATE, PUBLIC
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement