Here you can find the source of getPublicKey(byte[] keyBytes, String algorithm)
private static PublicKey getPublicKey(byte[] keyBytes, String algorithm)
//package com.java2s; //License from project: Open Source License import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.spec.EncodedKeySpec; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; public class Main { private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) { PublicKey publicKey = null; try {/* ww w .ja v a 2 s. co m*/ 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; } }