Here you can find the source of getPublicKey(String filename)
public static PublicKey getPublicKey(String filename)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; public class Main { public static PublicKey getPublicKey(String filename) { try {/*from w ww. ja va 2 s . com*/ final byte[] keyBytes = Files.readAllBytes(Paths.get(filename)); final X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); return factory().generatePublic(spec); } catch (IOException ex) { throw new RuntimeException("Error loading public key '" + filename + "'.", ex); } catch (final InvalidKeySpecException ex) { throw new RuntimeException("Error reading public key '" + filename + "'.", ex); } } private static KeyFactory factory() { try { return KeyFactory.getInstance("RSA"); } catch (final NoSuchAlgorithmException ex) { throw new RuntimeException("Could not find the RSA algorithm.", ex); } } }