我搜索了论坛和答案,但我无法弄清楚我的问题,我正在尝试验证签名,但它总是返回false,我做错了什么?我生成密钥,签名然后验证它(字节数组不为空)
public void Keygen() throws java.rmi.RemoteException, NoSuchAlgorithmException, IOException, SignatureException, NoSuchProviderException, InvalidKeyException {
KeyPairGenerator Keygen = KeyPairGenerator.getInstance("DSA");
Keygen.initialize(1024, random);
KeyPair pair = Keygen.generateKeyPair();
priv = pair.getPrivate();
pub = pair.getPublic();
}
public byte [] sign (int k)throws java.rmi.RemoteException, NoSuchAlgorithmException, SignatureException, InvalidKeyException , NoSuchProviderException
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
String data = "aa";
byte[] b = data.getBytes();
dsa.update(b);
realSig = dsa.sign();
key = pub.getEncoded();
return realSig;
}
public int Versig(byte [] sigkeys) throws java.rmi.RemoteException, NoSuchAlgorithmException, IOException, SignatureException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException{
byte [] pkb = getenckey();
KeyFactory kf = KeyFactory.getInstance("DSA");
PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(pkb));
/* create a Signature object and initialize it with the public key */
Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
sig.initVerify(pubKey);
String data2 = "bb";
byte[] c = data2.getBytes();
sig.update(c);
boolean verifies = sig.verify(sigkeys);
System.out.println("1 " + verifies);
if (verifies == true) {
System.out.println(" 2 " + verifies);
return (1);
} else {
return (2);
}
答案 0 :(得分:1)
您在验证String data = "aa";
时签名方法String data2 = "bb";
,因此签名验证将返回false。
对数据进行数字签名以检查数据的完整性(这意味着不会对数据进行更改)。
我希望这可以提供帮助!