public static KeyPair privateKeyToKeyPair(PrivateKey pKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
  KeyFactory keyFactory=KeyFactory.getInstance(KeyConstants.KEY_FACTORY,new BouncyCastleProvider());
  RSAPrivateCrtKey rsaPKey=(RSAPrivateCrtKey)pKey;
  RSAPublicKeySpec publicKeySpec=new java.security.spec.RSAPublicKeySpec(rsaPKey.getModulus(),rsaPKey.getPublicExponent());
  return new KeyPair(keyFactory.generatePublic(publicKeySpec),pKey);
}

public static KeyPair privateKeyToKeyPair ( PrivateKey pKey ) throws NoSuchAlgorithmException , InvalidKeySpecException { KeyFactory keyFactory = KeyFactory . getInstance ( KeyConstants . KEY_FACTORY , new BouncyCastleProvider ( ) ) ; RSAPrivateCrtKey rsaPKey = ( RSAPrivateCrtKey ) pKey ; RSAPublicKeySpec publicKeySpec = new java . security . spec . RSAPublicKeySpec ( rsaPKey . getModulus ( ) , rsaPKey . getPublicExponent ( ) ) ; return new KeyPair ( keyFactory . generatePublic ( publicKeySpec ) , pKey ) ; }
converts a given rsa privatekey instance to a keypair instance . the publickey will be extracted from privatekey instance .
