2

I'm write program Digitalsignature with java now i can send public key and signature to receiver But when receiver received my public key and signature

it's Type Of String(Base64) (i need to send String data)

How to revert String(Base64) to PublicKey(Type) again

public verifiSign(String signature,String data)  {
String publickey="MIG...."


    Signature sig = Signature.getInstance("SHA1withRSA");

    sig.initVerify(publickey); //<-- Cannot  use String
    sig.update(data.getBytes());
    boolean verified = sig.verify(asBytes(signature));
    System.out.println("Verify = " + verified);



}

Please help me Thank you

leppie
  • 115,091
  • 17
  • 196
  • 297
Puchong
  • 93
  • 2
  • 6
  • How did you transform the public key into a Base64 string? The same operation should just be reversed. The same classes should probably be used. – JB Nizet Jun 05 '12 at 09:13

2 Answers2

0

You can use this class to get a byte Array from the String:

http://www.docjar.com/docs/api/sun/misc/BASE64Decoder.html

import sun.misc.BASE64Decoder;

From the Byte Array, get a PublicKey Object... Btw. this code is not supported in the standart sdk, it is sun only, so be careful.

user1434045
  • 160
  • 1
  • 8
  • 1
    [It is a bad practice to use Sun's proprietary Java classes?](http://stackoverflow.com/questions/1834826/it-is-a-bad-practice-to-use-suns-proprietary-java-classes) – Jesper Jun 05 '12 at 09:27
  • You can get warnings like this: http://stackoverflow.com/questions/1136659/how-can-i-suppress-java-compiler-warnings-about-sun-proprietary-api – user1434045 Jun 05 '12 at 09:38
  • ... another good link http://stackoverflow.com/questions/469695/decode-base64-data-in-java – user1434045 Jun 05 '12 at 09:53
0

You can use this to transform your String (encoded in Base64) in a PublicKey instance :

Note : I don't know how you encode your String in Base64, if you used the apache commons for example, use the "revert" method from the same API. For this example I used sun.misc.BASE64Decoder because the String publicKey is encoded with 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));
}
alain.janinm
  • 19,951
  • 10
  • 65
  • 112