2 人間で original_message を送信したい。アリスとボブとしましょう。これらの手順が署名の検証に正しいかどうかを知りたいです。
- Alice は自分の PrivateKey で original_message をハッシュします -> h(m)
- アリスはハッシュ化されたメッセージを暗号化します -> c(h(m))
- Alice は自分の PrivateKey でメッセージに署名します -> s(c(h(m)))
Alice は最終的に署名されたメッセージ (PublicKey) と (original_message) を Bob に送信します。ボブ側:
- Bob は original_message をハッシュします -> h(m)
- ボブは署名されたメッセージをアリスの公開鍵で解読します -> d(s(c(h(m))))
- それらが等しいかどうかボブはハッシュされたメッセージで解読されたメッセージをチェックしますか? もし ( h(m) == d(s(c(h(m)))) )
私はいくつかの間違いをしていることを知っています。両側の正しい順序を知っている人はいますか?
ここでは、これを行うために java.security を使用しましたが、最終ステップでハッシュをチェックすると、 false が返されます!
アリスの部分で:
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;
}
}