これを使用して、(Base64 でエンコードされた) String を PublicKey インスタンスに変換できます。
注:Base64で文字列をエンコードする方法がわかりません。たとえば、Apacheコモンズを使用した場合は、同じAPIの「元に戻す」メソッドを使用してください。この例では、sun.misc.BASE64Decoder を使用しました。これは、String publicKey が sun.misc.BASE64Encoder でエンコードされているためです。
/**
* Verify the origin of the message using signature and encoded message.
* @param publicKey String - a public key, created with RSA and encoded with sun.misc.BASE64Encoder.
* @param sign String - a signature encoded with sun.misc.BASE64Encoder.
* @param message String - an encoded message.
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeySpecException
* @throws InvalidKeyException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchProviderException
* @throws IOException
* @throws SignatureException
* @see sun.misc.BASE64Encoder
*/
public boolean verifyMessageSign(String publicKey, String sign, String message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, IOException, SignatureException{
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//Create the PublicKey object from the String encoded in Base64.
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(publicKey));
PublicKey pub = keyFactory.generatePublic(publicKeySpec);
Signature sig = Signature.getInstance("SHA1withRSA");
sig.initVerify(pub);
sig.update(message.getBytes());
return sig.verify(new BASE64Decoder().decodeBuffer(sign));
}