我试图用私钥签名加密的消息并使用Java验证它。这是我第一次使用加密和签名,所以我不确定它应该如何工作,我有点卡在这里。验证总是返回false。无法验证Android上的rsa签名
在这里,我对消息进行签名:
public byte[] rsaSign (byte[] data) {
byte[] cipherData = null;
try {
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(signModulus, signExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey privKey = fact.generatePrivate(keySpec);
Signature s = Signature.getInstance("SHA1withRSA");
s.initSign(privKey);
s.update(data);
return s.sign();
}
return cipherData;
}
在这里,我试图验证签名:
public boolean rsaVerify (byte[] data, byte[] signature) {
boolean success = false;
try {
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(signModulus, signPublicExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
Signature s = Signature.getInstance("SHA1withRSA");
s.initVerify(pubKey);
s.update(data);
success = s.verify(signature);
return success;
}
return false;
}
任何人都可以看到的一个问题?密钥在C#中生成并转换为Java中的BigIntegers。
对我来说,这看起来很像一个图书馆相关的问题,如Ilmari所提到的那样,在SO上会好很多。您应该强烈考虑迁移您的问题,因为它很可能会在SO上得到解答。 – SEJPM
我没有发布它,所以我没有得到任何答案。我会考虑让它自成体系并再次尝试。谢谢! – user3685322
@ user3685322:确保将输入值发布到任何构建signModulus,signExponent,signPublicExponent;问题可能在那里。 – fgrieu