A sends A message to B. A encrypts the information with the password of A, sends the encrypted string together with the original text to B, and then B decrypts it with the password of B, then, determine whether the decrypted string is consistent with the original text sent by A. The key issue is,
The passwords A and B are different. This is the essence of digital signatures. A's password is the private key, and B's password is the public key.
Procedure:
When A private key and public key are generated, A uses the private key for encryption. Because the private key of A is only owned by A, the encrypted string is the signature string of, then A sends the signature string and the original text to B,
B obtains the encrypted string and decrypts it with the public key. Then, it determines whether the decrypted string is consistent with the original one. It indicates that it is signed by A. Otherwise, it is not signed by.
If you are still confused, you can see the following example and the text above.
For example, if the private key is S1 and the Public Key is G1 A in the original text: 123, then the process is
S1 + 123 encrypt to generate A signature string: AXXXX
A sends AXXXX and 123 together to B, B decrypts AXXXX with G1, and determines whether the decrypted string is equal to 123.
I believe that everyone understands the principles... Just understand the principle ,. Haha... For others, go to the code... Code is to learn from others .. Tested.
Aspx code:
Copy codeThe Code is as follows: <form id = "form1" runat = "server">
Random key generation: <asp: Button ID = "btncreateMY" runat = "server" Text = "random key generation" OnClick = "btncreateMY_Click"/> <br/>
Public Key: <asp: textBox ID = "tbxcreateMY_publicKey" runat = "server" TextMode = "MultiLine" Height = "59px" ReadOnly = "True" Width = "711px"> </asp: textBox> <br/>
Private Key: <asp: textBox ID = "tbxcreateMY_key" runat = "server" TextMode = "MultiLine" Height = "59px" ReadOnly = "True" Width = "710px"> </asp: textBox> <br/> <Br/>
Generate a signature: <br/>
Original article:
<Asp: TextBox ID = "tbxContent" runat = "server" TextMode = "MultiLine" Height = "59px" Width = "711px"> </asp: TextBox> <br/>
Private Key:
<Asp: TextBox ID = "tbxKey" runat = "server" TextMode = "MultiLine" Height = "59px" Width = "711px"> </asp: TextBox> <br/>
Signature:
<Asp: TextBox ID = "tbxSign" runat = "server" TextMode = "MultiLine" Height = "59px" ReadOnly = "True" Width = "711px"> </asp: textBox>
<Br/>
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "generate signature"/>
<Br/>
<Br/> <Br/>
Verify the signature: <br/>
Original article: <asp: TextBox ID = "tbxContentYZ" runat = "server" TextMode = "MultiLine" Height = "59px" Width = "711px"> </asp: textBox> <br/>
Public Key: <asp: TextBox ID = "tbxPublickeyYZ" runat = "server" TextMode = "MultiLine" Height = "59px" Width = "711px"> </asp: textBox> <br/>
Signature: <asp: TextBox ID = "tbxSignYZ" runat = "server" TextMode = "MultiLine" Height = "59px" Width = "711px"> </asp: TextBox>
<Br/>
<Asp: Button ID = "Button3" runat = "server" OnClick = "Button3_Click" Text = "Verify signature"/>
</Form>
CS codeCopy codeThe Code is as follows: // <summary>
/// Generate a signature
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void button#click (object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider ();
Objdsa. FromXmlString (tbxKey. Text );
Byte [] source = System. Text. UTF8Encoding. UTF8.GetBytes (tbxContent. Text );
// Digital Signature
TbxSign. Text = BitConverter. ToString (objdsa. SignData (source ));
}
/// <Summary>
/// Generate a random key
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btncreateMY_Click (object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider ();
TbxcreateMY_publicKey.Text = objdsa. ToXmlString (false );
TbxcreateMY_key.Text = objdsa. ToXmlString (true );
}
/// <Summary>
/// Verify the signature
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void Button3_Click (object sender, EventArgs e)
{
DSACryptoServiceProvider objdsa = new DSACryptoServiceProvider ();
Byte [] fileHashValue = new SHA1CryptoServiceProvider (). ComputeHash (System. Text. UTF8Encoding. UTF8.GetBytes (tbxContentYZ. Text ));
String [] strSplit = tbxSignYZ. Text. Split ('-');
Byte [] SignedHash = new byte [strSplit. Length];
For (int I = 0; I <strSplit. Length; I ++)
SignedHash [I] = byte. Parse (strSplit [I], System. Globalization. NumberStyles. AllowHexSpecifier );
Objdsa. FromXmlString (tbxPublickeyYZ. Text );
Bool ret = objdsa. VerifySignature (fileHashValue, SignedHash );
Response. Write (ret. ToString ());
// Qcd. Core. Web. Messages. ShowDialog (ret. ToString ());
}