我想在两个人之间发送 original_message。假设 Alice 和 Bob,我想知道这些步骤对于验证签名是否正确
- Alice 用她的 PrivateKey 散列 original_message -> h(m)
- Alice 加密散列消息 -> c(h(m))
- Alice 使用她的 PrivateKey -> s(c(h(m))) 签署消息
Alice 将带有她的 (PublicKey) 和 (the original_message) 的最终签名消息发送给 Bob。在鲍勃方面:
- Bob 散列 original_message -> h(m)
- Bob 用 Alice 公钥解密签名消息 -> d(s(c(h(m))))
- Bob用散列消息检查解密消息是否相等?如果 ( h(m) == d(s(c(h(m)))) )
我知道我做错了。有谁知道双方的好顺序是什么?
在这里,我使用 java.security 来执行此操作,但是当我在最后一步检查哈希时,它给了我错误!
在爱丽丝部分:
public byte[] Sign(byte[] aMessage) {
try {
// get an instance of a cipher with RSA with ENCRYPT_MODE
// Init the signature with the private key
// Compute signature
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, thePrivateKey);
Signature instance = Signature.getInstance("MD5withRSA");
instance.initSign(thePrivateKey);
// get an instance of the java.security.MessageDigest with MD5
// process the digest
MessageDigest md5_digest = MessageDigest.getInstance("MD5");
md5_digest.update(aMessage);
byte[] digest = md5_digest.digest();
// return the encrypted digest
byte[] cipherText = cipher.doFinal(digest);
instance.update(cipherText);
byte[] signedMSG = instance.sign();
return signedMSG;
} catch (Exception e) {
System.out.println("Signature error");
e.printStackTrace();
return null;
}
}
在鲍勃部分:
public boolean CheckSignature(byte[] aMessage, byte[] aSignature,
PublicKey aPK) {
try {
// get an instance of a cipher with RSA with ENCRYPT_MODE
// Init the signature with the private key
// decrypt the signature
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, aPK);
byte[] decrypted_digest = cipher.doFinal(aSignature);
// get an instance of the java.security.MessageDigest with MD5
MessageDigest md5_digest = MessageDigest.getInstance("MD5");
// process the digest
md5_digest.update(aMessage);
byte[] digest = md5_digest.digest();
// check if digest1 == digest2
if (decrypted_digest == digest) {
return true;
}else {
return false;
}
} catch (Exception e) {
System.out.println("Verify signature error");
e.printStackTrace();
return false;
}
}