C # Programming Summary (eight) digital signature

Source: Internet
Author: User

C # Programming Summary (eight) digital signature

In the daily work, there are many documents need to lead review, signature and seal, as the company's business, cross-regional, cross-border business is increasingly common, the leadership signature and seal become very troublesome, the beginning of the people by mail, fax and other ways to solve, but the cost of time, manpower, material resources. In the increasingly deepening of the network today, need to lead the approval, signing and stamping things more and more, time is more and more urgent, the emergence of digital signatures, is a good solution to this problem. Promote the development of the Internet and multinational groups.

Digital signatures1. Concept

The digital signature is based on the hashing algorithm and public key encryption algorithm, the hash algorithm is used to calculate the digest first, then the digest is encrypted with the private key, and the obtained value is the digital signature of the original text.

Digital signature (also known as public key digital signature, electronic seal) is a kind of common physical signature written on paper, but it uses the technology of public key cryptography to identify the digital information method.

A set of digital signatures typically defines two complementary operations, one for signing and the other for validation.

Can be used to represent the use of electronic signatures:

2. Working principle

The flowchart is as follows:

The use of digital signatures generally involves the following steps, which we introduce in a secure e-mail case

(1) Send a piece of life or get a unique encryption cipher group, including the private key and public key.

(2) The sender writes an e-mail

(3) sender uses a secure digest algorithm to get a summary of e-mail messages

(4) The sender then uses the private key to encrypt the information digest to obtain a digital signature.

(5) The sender appended the digital signature to the information.

(6) The sender sends a digital signature and information (encrypted or unencrypted) to an electronic recipient.

(7) The recipient uses the sender's public password (public key) to confirm the sender's electronic signature, and the sender's digital signature is decrypted by the public key to obtain a summary of the information

(8) The recipient uses the same secure digest algorithm to get information (encrypted or unencrypted) of the "Information Digest".

(9) The recipient compares two summaries of information. If the two are the same, the recipient can be confident that the information has not changed since it was issued

(10) The recipient obtains the certificate from the certifying agency (or obtained by the sender of the information), which confirms the authenticity of the digital signature on the sender's message. The proving institution is a typical third party in the digital signature system which is entrusted with the management attestation business. The certificate contains the sender's public password and name ( and other possible additional information) are digitally signed by the certifying authority.

where, (1) ~ (6) is the production process of digital signature, (7) ~ (10) is the verification process of digital signature

3, the main role

1, anti-impersonation, because the private key only the signer know, others can not forge the correct signature
2, identification, the receiver with the sender's public key to the message signature decryption operation, and the original text to match
3, anti-tamper, clear text and signature values sent together, mutual verification, to prevent data tampering
4, anti-repudiation, signature value can identify identity, with the signature of the data contained in the information signer is not to deny

4. Application

Now typical applications such as: Online Banking, e-commerce, e-Government, network communications, etc.

One of the most important application scenarios is: digital certificates, followed by specific articles on which they are described in detail.

5. Realize digital signature via RSA

Digital signatures

        <summary>///digital signature///</summary>//        <param name= "PlainText" > Original </param> //        <param name= "Privatekey" > Private key </param>//        <returns> signature </returns> public        static string hashandsignstring (string plaintext, string privatekey)        {            unicodeencoding byteconverter = new UnicodeEncoding ();            byte[] Datatoencrypt = byteconverter.getbytes (plaintext);            using (RSACryptoServiceProvider rsaalg = new RSACryptoServiceProvider ())            {                rsaalg.fromxmlstring ( Privatekey);                Use SHA1 for digest algorithm, generate signature                byte[] EncryptedData = Rsaalg.signdata (Datatoencrypt, New SHA1CryptoServiceProvider ());                Return convert.tobase64string (EncryptedData);            }        }

Signature authentication:

        <summary>///verification signature///</summary>//        <param name= "PlainText" > Original </param> //        <param name= "Signeddata" > Signature </param>//        <param name= "PublicKey" > Public key </param>        //<returns></returns> public static bool Verifysigned (string plaintext, string signeddata, String publickey)        {            using (RSACryptoServiceProvider rsaalg = new RSACryptoServiceProvider ())            {                Rsaalg.fromxmlstring (PublicKey);                UnicodeEncoding byteconverter = new UnicodeEncoding ();                byte[] Datatoverifybytes = byteconverter.getbytes (plaintext);                byte[] signeddatabytes = convert.frombase64string (signeddata);                Return Rsaalg.verifydata (Datatoverifybytes, New SHA1CryptoServiceProvider (), signeddatabytes);            }        }

Test Case:

        public static void Testsign ()        {            string originaldata = "The article is good, this is my signature: Obama!" ";            Console.WriteLine ("Number of signatures: {0}", originaldata);            keyvaluepair<string,string> KeyPair = Encrypter.creatersakey ();            string privatekey = Keypair.value;            string publickey = Keypair.key;            1, generate the signature, through the digest algorithm            string signeddata = Encrypter.hashandsignstring (Originaldata, privatekey);            Console.WriteLine ("Digital signature: {0}", signeddata);            2. Verify signature            bool verify = encrypter.verifysigned (Originaldata, signeddata,publickey);            Console.WriteLine ("Signature Verification result: {0}", verify);        }

Source Address:

Http://files.cnblogs.com/yank/EncriptSample.zip

C # Programming summary (eight) digital signatures

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.