0

我想在两个人之间发送 original_message。假设 Alice 和 Bob,我想知道这些步骤对于验证签名是否正确

  1. Alice 用她的 PrivateKey 散列 original_message -> h(m)
  2. Alice 加密散列消息 -> c(h(m))
  3. Alice 使用她的 PrivateKey -> s(c(h(m))) 签署消息

Alice 将带有她的 (PublicKey) 和 (the original_message) 的最终签名消息发送给 Bob。在鲍勃方面:

  1. Bob 散列 original_message -> h(m)
  2. Bob 用 Alice 公钥解密签名消息 -> d(s(c(h(m))))
  3. 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;
        }
    }
4

2 回答 2

0

最后我找到了答案。错误是,在 Alice 部分做一个 sign() 。因为当你进行散列和加密时,它已经变成了一个签名,而当你再次对它进行签名()时,鲍勃的部分变得不可能恢复散列签名。

我的代码也是“Java 安全公钥加密中的单向散列”的一个很好的例子

这是 Alice 部分的修改,之后一切正常。

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);

            // get an instance of the java.security.MessageDigest with MD5
            // process the digest
            MessageDigest md5_digest = MessageDigest.getInstance("MD5");
            byte[] digest = md5_digest.digest(aMessage);

            // return the encrypted digest
            byte[] cipherText = cipher.doFinal(digest);

            return cipherText;

        } catch (Exception e) {
            System.out.println("Signature error");
            e.printStackTrace();
            return null;
        }

    }
于 2014-01-09T10:28:15.803 回答
-2

数字签名是消息哈希的加密(带有私钥)密码。

签名 s = c(h(m))

现在 s 附加到消息 m 上。从 Alice 发送给 Bob 的签名消息是 m+s

在 Bob 一方收到 m+s 后,Bob 将使用 Alice 的公钥解密签名,该公钥将出现在证书中。所以他在这里做 d(s) = d(c(h(m)) = h(m)

Bob 也收到了消息,因此他将计算消息 m 的哈希值,即 h(m)

现在他将比较上述两个步骤的输出,看看它们是否匹配。这确保了消息没有被中间的任何人篡改。

这是数字签名如何工作的一般概念。希望这可以帮助。

维基百科在这里有相同过程的图形表示:http: //upload.wikimedia.org/wikipedia/commons/2/2b/Digital_Signature_diagram.svg

于 2014-01-08T23:21:30.443 回答