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.nio.file.Files; import java.io.File; import java.security.KeyFactory; import java.security.PublicKey; import java.security.spec.EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; public class Main { public static PublicKey getPublicKey(String filename) { try {// w w w. j a v a 2s .c om byte[] keyBytes = Files.readAllBytes(new File(filename).toPath()); EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); } catch (Exception e) { e.printStackTrace(); } return null; } public static PublicKey getPublicKey(File file) { try { byte[] keyBytes = Files.readAllBytes(file.toPath()); EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); } catch (Exception e) { e.printStackTrace(); } return null; } }