zoukankan      html  css  js  c++  java
  • How does a public key verify a signature? 公钥如何验签的

    核心概念还是,

    公钥加密的东西,可以使用私钥解密。

    而反过来,私钥加密的东西,也可以用公钥进行解密。

     

    通过hash函数处理文本,得到摘要digest。

    然后把摘要digest用私钥进行加密,得到数字签名digital signature。这里加密的目的,是为了防止篡改。(因为如果不把摘要加密,直接发出去的话,可能会被篡改)

    收到数字签名的话,用公钥解密,得到摘要digest1。

    然后用hash计算文本内容,得到另外一个摘要digest2。

    只要digest1和digest2是相同的话。就可以确认文本是原来的。

    How does a public key verify a signature?

    I am trying to get a better grapple on how public/private keys work. I understand that a sender may add a digital signature to a document using his/her private key to essentially obtain a hash of the document, but what I do not understand is how the public key can be used to verify that signature.

    My understanding was that public keys encrypt, private keys decrypt... can anyone help me understand?

    回答1

    Your understanding of "public keys encrypt, private keys decrypt" is correct... for data/message ENCRYPTION. For digital signatures, it is the reverse. With a digital signature, you are trying to prove that the document signed by you came from you. To do that, you need to use something that only YOU have: your private key.

    A digital signature in its simplest description is a hash (SHA1, MD5, etc.) of the data (file, message, etc.) that is subsequently encrypted with the signer's private key. Since that is something only the signer has (or should have) that is where the trust comes from. EVERYONE has (or should have) access to the signer's public key.

    So, to validate a digital signature, the recipient

    1. Calculates a hash of the same data (file, message, etc.),
    2. Decrypts the digital signature using the sender's PUBLIC key, and
    3. Compares the 2 hash values.

    If they match, the signature is considered valid. If they don't match, it either means that a different key was used to sign it, or that the data has been altered (either intentionally or unintentionally).

    Hope that helps!

    评论:

    My understanding was that the keys were not symmetric... that is, objects encrypted with a public key are able to be decrypted by the private key, but that this relationship did not work inversely... more specifically, I did not think objects encrypted with the private key could be decrypted by the public key. If that is indeed the case, than this definitely answers my question. Aug 15 '13 at 18:55
     
    The keys work inversely to each other. Encrypted something with your public key? Decrypt it with your private key. Conversely, if you encrypted something with your private key, you decrypt it with your public. Such is the nature of asymmetric cryptography. Aug 15 '13 at 19:12
     
    Symmetric just means that the same key is used to encrypt/decrypt. Assymetric means that one key encrypts and a different key decrypts (and that the reverse is also true).
    – gtrig
    Aug 19 '13 at 5:27
     
    @Jodimoro, Technically a message is NOT "Secret" if it's encrypted with a private key. If it's encrypted with a private key anyone with the publicially available "public" key can decrypt the message. May 1 '18 at 18:04
     
    @Jodimoro The only reason the hash is encrypted with a private key into a signature is to ensure the hash is not changed... not to ensure it's "secret". May 1 '18 at 18:16

     回答2

    The keys work inversely:

    Public key encrypts, private key decrypts (encrypting):

    openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt -out message.ssl
    openssl rsautl -decrypt -inkey private.pem       -in message.ssl -out message.txt
    

    Private key encrypts, public key decrypts (signing):

    openssl rsautl -sign -inkey private.pem       -in message.txt -out message.ssl
    openssl rsautl       -inkey public.pem -pubin -in message.ssl -out message.txt
    

    Below is an example script to test this whole flow with openssl.

    #!/bin/sh
    # Create message to be encrypted
    echo "Creating message file"
    echo "---------------------"
    echo "My secret message" > message.txt
    echo "done\n"
    
    # Create asymmetric keypair
    echo "Creating asymmetric key pair"
    echo "----------------------------"
    openssl genrsa -out private.pem 1024
    openssl rsa -in private.pem -out public.pem -pubout
    echo "done\n"
    
    # Encrypt with public & decrypt with private
    echo "Public key encrypts and private key decrypts"
    echo "--------------------------------------------"
    openssl rsautl -encrypt -inkey public.pem -pubin -in message.txt         -out message_enc_pub.ssl
    openssl rsautl -decrypt -inkey private.pem       -in message_enc_pub.ssl -out message_pub.txt
    xxd message_enc_pub.ssl # Print the binary contents of the encrypted message
    cat message_pub.txt # Print the decrypted message
    echo "done\n"
    
    # Encrypt with private & decrypt with public
    echo "Private key encrypts and public key decrypts"
    echo "--------------------------------------------"
    openssl rsautl -sign    -inkey private.pem -in message.txt          -out message_enc_priv.ssl
    openssl rsautl -inkey public.pem -pubin    -in message_enc_priv.ssl -out message_priv.txt
    xxd message_enc_priv.ssl
    cat message_priv.txt
    echo "done\n"
    

    This script outputs the following:

    Creating message file
    ---------------------
    done
    
    Creating asymmetric key pair
    ----------------------------
    Generating RSA private key, 1024 bit long modulus
    ...........++++++
    ....++++++
    e is 65537 (0x10001)
    writing RSA key
    done
    
    Public key encrypts and private key decrypts
    --------------------------------------------
    00000000: 31c0 f70d 7ed2 088d 9675 801c fb9b 4f95  1...~....u....O.
    00000010: c936 8cd0 0cc4 9159 33c4 9625 d752 5b77  .6.....Y3..%.R[w
    00000020: 5bfc 988d 19fe d790 b633 191f 50cf 1bf7  [........3..P...
    00000030: 34c0 7788 efa2 4967 848f 99e2 a442 91b9  4.w...Ig.....B..
    00000040: 5fc7 6c79 40ea d0bc 6cd4 3c9a 488e 9913  _.ly@...l.<.H...
    00000050: 387f f7d6 b8e6 5eba 0771 371c c4f0 8c7f  8.....^..q7.....
    00000060: 8c87 39a9 0c4c 22ab 13ed c117 c718 92e6  ..9..L".........
    00000070: 3d5b 8534 7187 cc2d 2f94 0743 1fcb d890  =[.4q..-/..C....
    My secret message
    done
    
    Private key encrypts and public key decrypts
    --------------------------------------------
    00000000: 6955 cdd0 66e4 3696 76e1 a328 ac67 4ca3  iU..f.6.v..(.gL.
    00000010: d6bb 5896 b6fe 68f1 55f1 437a 831c fee9  ..X...h.U.Cz....
    00000020: 133a a7e9 005b 3fc5 88f7 5210 cdbb 2cba  .:...[?...R...,.
    00000030: 29f1 d52d 3131 a88b 78e5 333e 90cf 3531  )..-11..x.3>..51
    00000040: 08c3 3df8 b76e 41f2 a84a c7fb 0c5b c3b2  ..=..nA..J...[..
    00000050: 9d3b ed4a b6ad 89bc 9ebc 9154 da48 6f2d  .;.J.......T.Ho-
    00000060: 5d8e b686 635f b6a4 8774 a621 5558 7172  ]...c_...t.!UXqr
    00000070: fbd3 0c35 df0f 6a16 aa84 f5da 5d5e 5336  ...5..j.....]^S6
    My secret message
    done
    
  • 相关阅读:
    11计划
    Tomcat Server.xml配置详解
    maven常用配置
    [转]Maven的内置属性说明
    PL/SQL Developer使用技巧、快捷键
    01_jeecms建站
    01_bootStrap中Tab页签切换
    利用Java实现文件中的关键字查询
    SVN服务器搭建
    MyEclipse安装插件的几种方法
  • 原文地址:https://www.cnblogs.com/chucklu/p/15684237.html
Copyright © 2011-2022 走看看