3
我試圖在java中籤名消息,但它似乎不起作用。驗證步驟讓我錯誤。RSA在java中籤名和驗證
誰能告訴我我的錯誤在哪裏。我無法理解我做錯了什麼。 感謝
String messageString = "text";
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
keyGen.initialize(512, new SecureRandom());
KeyPair keyPair = keyGen.generateKeyPair();
PublicKey RSAPublicKey = keyPair.getPublic();
PrivateKey RSAPrivateKey = keyPair.getPrivate();
System.out.println("public key = " + RSAPublicKey);
System.out.println("private key = " + RSAPrivateKey);
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Signature signature = Signature.getInstance("SHA1withRSA", "BC");
signature.initSign(RSAPrivateKey, new SecureRandom());
byte[] message = messageString.getBytes();
signature.update(message);
byte[] sigBytes = signature.sign();
Signature signature1 = Signature.getInstance("SHA1withRSA", "BC");
signature1.initVerify(RSAPublicKey);
signature1.update(sigBytes);
boolean result = signature1.verify(sigBytes);
System.out.println("result = "+result);
} catch (NoSuchAlgorithmException | NoSuchProviderException | SignatureException | InvalidKeyException ex) {
} }
你應該寫'signature1.update(消息);'代替'signature1.update(sigBytes);'。在你已經顯示的代碼中,signature1沒有關於數據的信息來驗證你的簽名。 –