Personal Understanding the application scenarios of C # symmetric encryption asymmetric Cryptographic hashing algorithm

Source: Internet
Author: User
Tags md5 encryption asymmetric encryption

C # class Library by default implements a series of cryptographic algorithms in System.Security.Cryptography; Under namespaces

Symmetric encryption

Encrypted and decrypted by the same key. Often used in the case of internal data transfer. such as company a program and B Program. A program to send data to the B program but to prevent the sending of data in plaintext to be Stolen. Then I'm going to set a Protocol. the byte byte of the transmitted data is unified +1 and the data is received in the case of the byte byte unified-1 then can obtain the correct data (of course this is a simple encryption), the real application of encryption is certainly more complex than this

Asymmetric encryption

Both sides of the transmitting data have their own public key and private key public key to keep the private key themselves, when the data like B user sends the data using the B user's public key to encrypt the encrypted information only B User's private key can be decrypted then if the message is intercepted during transmission, then it can't be cracked.

For example, the payment operation a user enters the bank card number and the password to pay then in the transmission process if not capture packet interception Data. is very dangerous. Then uniform encrypts the parameter information on the client through the public key provided by the Server. If it is intercepted during transmission

We all know that HTTPS requests are safe in the process of transmitting Data.

The HTTPS request client needs to install the certificate certificate just like the public and private key client initiates a request to obtain the Server's public key and sends its own public key to the Server. The security of data is ensured by using both public key for encrypted transmission during Transmission.

The difference between an HTTPS request and a normal request is that HTTPS is a long connection and increases both the handshake of the public key exchange

Liezi

usingSystem.Collections.Generic;usingsystem.io;usingsystem.linq;usingsystem.text;usingSystem.Threading.Tasks;usingSystem.Security.Cryptography;namespacesecuretranfertest{classprogram {/// <summary>        ///Annie's Private Spoon./// </summary>         public StaticSystem.Security.Cryptography.CngKey Anlikey =NULL; //Anne's Public Key         public Static byte[] Anlipulickey =NULL; //Bob's Private Key.         public StaticSystem.Security.Cryptography.CngKey Bobkey =NULL;  public Static byte[] Bobpulickey =NULL; Static voidMain (string[] Args)            {CreateKey (); Anlisendmessage ("It's a fine day today.");//Annie starts sending a message to BobAnlisendmessage ("La la la");//Annie starts sending a message to BobConsole.readkey (); }         public Static voidCreateKey () {//Create a private key with ECDsaP256Anlikey =cngkey.create (cngalgorithm.ecdiffiehellmanp256); //generate public key based on private keyAnlipulickey =anlikey.            Export (cngkeyblobformat.eccpublicblob); //Create a private key with ECDsaP256Bobkey =cngkey.create (cngalgorithm.ecdiffiehellmanp256); //obtaining a private key based on the public keyBobpulickey =bobkey.        Export (cngkeyblobformat.eccpublicblob); }         public  Static voidAnlisendmessage (stringMessage) {            byte[] RowData = Encoding.UTF8.GetBytes (message);//convert sent messages to binary format//Create a new random key pair with Annli's private key            using(ECDIFFIEHELLMANCNG CNG =Newecdiffiehellmancng (anlikey)) {                //get a Cngkey Key object via Bob's public key byte[]                using(CngKey Bobkey =Cngkey.import (bobpulickey, cngkeyblobformat.eccpublicblob)) {                    //generate a symmetric key with Bob's public key through the Anli key pair                    varSumkey =Cng.                    Derivekeymaterial (bobkey); //Create a symmetric encryption and decryption (AEC) advanced algorithm implementation                    using(varAES =NewAescryptoserviceprovider ()) {aes. Key= sumkey;//to set a symmetric encryption keyAes.                        GenerateIV (); //generating symmetric and Sh-dense objects                        using(icryptotransform encryptor =Aes. CreateEncryptor ()) {using(memorystream ms =NewMemoryStream ()) {                                //define an encryption transformation stream                                varCS =NewCryptoStream (ms, encryptor, cryptostreammode.write); //write encryption initialization vector (IV)Ms. Write (aes.iv,0, Aes.iv.                                Length); //Write as pass dataCs. Write (rowdata,0, rowdata.length); Cs. Close ();//must be closed after use or data will be lost                               varData=Ms.                                ToArray (); //Annie sends the encrypted data message to Bob Binary data for encrypted data ()Console.WriteLine ("Annie sends the encrypted data message to Bob Binary data for encrypted data");                            Bobreceivesdata (data); } aes.                        Clear (); }                    }                }            }        }        //Bob receives information         public Static voidBobreceivesdata (byte[] Data) {console.writeline ("Bob receives and begins to decrypt"); byte[] RowData =NULL; //first read unencrypted initialization vector (IV) in data//1. Aescryptoserviceprovider obtains the length of IV through symmetric encryption advanced algorithm            using(varAES =NewAescryptoserviceprovider ()) {                varIvlength = Aes. BlockSize >>3;//his action block binary length is converted to a byte-stored decimal length                byte[] Ivdata =New byte[ivlength];                Array.copy (data, ivdata, ivlength); //Create a new random key pair with Bob's private key                using(ECDIFFIEHELLMANCNG CNG =Newecdiffiehellmancng (bobkey)) {                    //obtain a Cngkey Key object via Anni's public key byte[]                    using(CngKey Anikey =Cngkey.import (anlipulickey, cngkeyblobformat.eccpublicblob)) {                        //generate a symmetric key with Bob's public key through the Anli key pair                        varSumkey =Cng.                        Derivekeymaterial (anikey); Aes. Key= sumkey;//to set a symmetric encryption keyAES.IV =ivdata; using(icryptotransform decryptor =Aes. CreateDecryptor ())using(memorystream me =NewMemoryStream ()) {                           //define an encryption transformation stream                            varCS =NewCryptoStream (me, decryptor, cryptostreammode.write); Cs. Write (data, ivlength, Data. Length-ivlength);//decryption of encrypted informationCs. Close ();//be sure to close otherwise the last data will be lostRowData =Me.                            ToArray (); Console.WriteLine ("The decryption success Information Is:");                        Console.WriteLine (Encoding.UTF8.GetString (rowdata)); }                    }                }            }                           }    }}

Hash algorithm

The same string calculates the unique hash value by hashing the Algorithm. Like MD5 Encryption.

Personal Understanding the application scenarios of C # symmetric encryption asymmetric Cryptographic hashing algorithm

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.