我試圖用私鑰簽名加密的消息並使用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