Here you can find the source of getPublicKeySpec(KeyPair kp)
public static RSAPublicKeySpec getPublicKeySpec(KeyPair kp) throws NoSuchAlgorithmException, InvalidKeySpecException
//package com.java2s; //License from project: Open Source License import java.security.KeyFactory; import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.RSAPublicKeySpec; public class Main { public static RSAPublicKeySpec getPublicKeySpec(KeyPair kp) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory fact = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pub = fact.getKeySpec(kp.getPublic(), RSAPublicKeySpec.class); return pub; }/* w w w. j a va 2 s. c o m*/ public static RSAPublicKeySpec getPublicKeySpec(PublicKey pubkey) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory fact = KeyFactory.getInstance("RSA"); RSAPublicKeySpec pub = fact.getKeySpec(pubkey, RSAPublicKeySpec.class); return pub; } }