使用 java android 签名和验证

Signing and verifying with java android

我正在尝试使用私钥签署加密消息并在 Java 中对其进行验证。这是我第一次使用加密和签名,所以我不确定它应该如何工作,我有点被困在这里。验证总是 returns false.

我在这里发布代码,其中包含最重要的部分:

import android.util.Base64;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;

public class SignatureTest {

// testing signature
PublicKey javaPublicKey;
PrivateKey javaPrivateKey;

String message = "Hello world";


public void test() {

    try {
        GenerateKeys();
        byte[] messageBytes = message.getBytes("UTF-8");
        byte[] signature = rsaSign(messageBytes);
        boolean success = rsaVerify(messageBytes, signature);

        if(success){
            Log.e("yay", "yay");
        }
        else {
            Log.e("nay", "nay");
        }


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

}

public void GenerateKeys() {

    SecureRandom random = null;
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        random = SecureRandom.getInstance("SHA1PRNG");
        keyGen.initialize(1024, random);

        KeyPair pair = keyGen.generateKeyPair();

        javaPrivateKey = pair.getPrivate();
        javaPublicKey = pair.getPublic();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

public byte[] rsaSign (byte[] data) {

    byte[] cipherData = null;

    try {

        Signature s = Signature.getInstance("SHA1withRSA");
        s.initSign(javaPrivateKey);

        s.update(data);

        Log.e("s.sign()", Base64.encodeToString(s.sign(), Base64.DEFAULT));


        return s.sign();


    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }

    return cipherData;
}

public boolean rsaVerify (byte[] data, byte[] signature) {

    boolean success = false;

    try {

        Signature s = Signature.getInstance("SHA1withRSA");
        s.initVerify(javaPublicKey);

        s.update(data);

        success = s.verify(Base64.decode(signature, Base64.DEFAULT));

        if(success == true) {
            Log.i("yeay", "yay");
        }
        else {
            Log.i("nay", "nay");
        }

        return success;


    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }

    return false;
}
}

谁能告诉我我做错了什么?

rsaSign方法中,在行s.update(data);

之后执行以下操作
byte[] signature = s.sign();
Log.e("s.sign()", Base64.encodeToString(signature , Base64.DEFAULT));    
return signature;

请记住,调用 sign() 方法会将签名对象重置为之前通过调用 initSign(PrivateKey) 进行签名初始化时所处的状态。也就是说,对象被重置并可用于从同一签名者生成另一个签名(如果需要),通过新调用更新和签名。
现在,在 rsaVerify 方法中,替换行
success = s.verify(Base64.decode(signature, Base64.DEFAULT));
与,
success = s.verify(signature);