privatestatic PublicKey getPublicKey(byte[] keyBytes, String algorithm){ PublicKey publicKey = null; try { KeyFactory kf = KeyFactory.getInstance(algorithm); EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); publicKey = kf.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { System.out.println("Could not reconstruct the public key, the given algorithm could not be found."); } catch (InvalidKeySpecException e) { System.out.println("Could not reconstruct the public key"); }
return publicKey; }
privatestatic PrivateKey getPrivateKey(byte[] keyBytes, String algorithm){ PrivateKey privateKey = null; try { KeyFactory kf = KeyFactory.getInstance(algorithm); EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); privateKey = kf.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { System.out.println("Could not reconstruct the private key, the given algorithm could not be found."); } catch (InvalidKeySpecException e) { System.out.println("Could not reconstruct the private key"); }