asp.net digital Signature Instance Code _ Practical Skills

Source: Internet
Author: User
Tags decrypt
A to send a message to B, a will encrypt the information with a password, and then the encrypted string and the original text sent to B, and then B with the password to decrypt, and then judge the decrypted string and a sent over the original text is consistent, the key problem is that,
A and password and b password is not the same, this is the essence of digital signature, a password is the private key, B's password is the public key
Specific steps:
Generates a private key and a public key, a is encrypted with the private key because the private key of a has only a, so the encrypted string is the signature string of a, and then a sends the string and the original text of the signature to B,
b get the encrypted string with the public key to decrypt, and then judge the decrypted string and the original is consistent, consistent description is a signed, otherwise it is not a signature
If the look is still a bit confused, then read the following example, read the above text, it is estimated that the
For example: The private key is S1 and the public key is: G1 a The original is: 123 then this process is
S1+123 encryption generates a signed string of a: axxxx
A will axxxx and 1231 issued to b,b with G1 to decrypt the axxxx, to determine whether the decrypted string is equal to 123
Believe the principle that everyone understands ... Understand the principle is good,. Oh... The other does not say, on the code ... Code is learning from others. has been tested.
ASPX code:
Copy Code code as follows:

<form id= "Form1" runat= "Server" >
Randomly generated key: <asp:button id= "Btncreatemy" runat= "Server" text= "Random Generation Key" 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 Signature: <br/>
Original:
<asp:textbox id= "tbxcontent" runat= "Server" textmode= "MultiLine" height= "59px" width= "711px" ></asp:textbox > <br/>
Private:
<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= "Button1_Click" text= "Generate signature"/>
<br/>
<br/><br/>
Verify signature: <br/>
Original: <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 Code
Copy Code code as follows:

<summary>
Generate signature
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Button1_Click (object sender, EventArgs e)
{
DSACryptoServiceProvider OBJDSA = new DSACryptoServiceProvider ();
Objdsa. Fromxmlstring (Tbxkey.text);
byte[] Source = System.Text.UTF8Encoding.UTF8.GetBytes (Tbxcontent.text);
Digital signatures
Tbxsign.text = bitconverter.tostring (OBJDSA. SignData (source));
}
<summary>
Randomly generating keys
</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>
Verifying signatures
</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 ());
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.