2

私はJavaを使用してプログラムDigitalsignatureを作成しています。これで、公開鍵と署名を受信者に送信できますが、受信者が公開鍵と署名を受け取ったとき

Type Of String(Base64) です (文字列データを送信する必要があります)

String(Base64) を PublicKey(Type) に戻す方法

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);



}

助けてください ありがとう

4

2 に答える 2

0

このクラスを使用して、文字列からバイト配列を取得できます。

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

import sun.misc.BASE64Decoder;

バイト配列から、PublicKeyオブジェクトを取得します...ところで。このコードは標準のSDKではサポートされていません。Sunのみであるため、注意してください。

于 2012-06-05T09:16:43.183 に答える
0

これを使用して、(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));
}
于 2012-06-05T09:20:02.657 に答える