1. Symmetric encryption algorithm
Symmetric encryption is the fastest and simplest way to encrypt encryption (encryption) and decryption (decryption) with the same key (secret key).
Symmetric encryption has many algorithms, and because of its high efficiency, it is widely used in the core of many cryptographic protocols. Symmetric encryption typically uses a relatively small key,
Generally less than the bit. The greater the key, the stronger the encryption, but the slower the encryption and decryption process. If you use only 1 bit to do this key, the hacker can first try to decrypt with the
If you can't, then use 1 solutions, but if your keys are 1 MB large, hackers may never be able to crack them, but the encryption and decryption process takes a long time.
The size of the key is both to take care of the security, but also to take care of the efficiency, is a trade-off.
Commonly used symmetric encryption: DES, 3DES, AES, etc.
(The code is added later)
2. Asymmetric Encryption algorithm
Asymmetric encryption provides a very secure way to encrypt and decrypt data, using a pair of keys, public key, and private key.
The private key can only be safely kept by one party and cannot be compromised, while the public key may be sent to any person requesting it. Asymmetric encryption uses one of these keys to encrypt, while decryption requires another key.
For example, you ask the bank for the public key, the bank sends you the public key, you encrypt the message using the public key, then only the holder of the private key-the bank can decrypt your message.
Unlike symmetric encryption, the bank does not need to send the private key over the network, so security is greatly improved.
Commonly used asymmetric encryption: DSA, RSA, etc.
Currently available in C # RSA Asymmetric encryption (other languages such as Java can be reversed and decrypted, but not tested), can only use the public key to encrypt, can only use the private key to decrypt, can not reverse use (the private key cannot be encrypted, the public key can not be decrypted),
Because of this higher security, there is no data after the private key encryption, all public key holders can decrypt the problem, if it is necessary to have this need to appear, you can use
Third-party encryption and decryption components Bouncycastle to achieve
//Introducing NamespacesusingSystem;usingSystem.IO;usingSystem.Text;usingSystem.Security.Cryptography;//RSA Test InstancestringOldData ="Taiyonghai"; Creatersakey ();stringciphertext =Rsaencrypt (olddata);stringNewData =Rsadecrypt (ciphertext);/// <summary>///To create an RSA public key/// </summary> Public voidCreatersakey () {//set [public key private keys] file path stringPrivatekeypath =@"D:\\privatekey.xml"; stringPublickeypath =@"D:\\publickey.xml"; //Create an RSA objectRSACryptoServiceProvider RSA =NewRSACryptoServiceProvider (); //generate rsa[Public key private key] stringPrivatekey = RSA. Toxmlstring (true); stringPublicKey = RSA. Toxmlstring (false); //writes the key to the specified pathFile.writealltext (Privatekeypath, Privatekey);//The file contains the public and private keysFile.writealltext (Publickeypath, PublicKey);//only the public key is included in the file}/// <summary>///using RSA for encryption/// </summary>/// <param name= "Data" >Encrypt Data</param>/// <returns></returns> Public stringRsaencrypt (stringdata) { //C # can only be encrypted using [public key] By default (want to use [public key decryption] can be implemented using third-party component Bouncycastle) stringPublickeypath =@"D:\\publickey.xml"; stringPublicKey =File.readalltext (Publickeypath); //Create an RSA object and load [public key]RSACryptoServiceProvider rsapublic =NewRSACryptoServiceProvider (); Rsapublic.fromxmlstring (PublicKey); //Encrypt the data byte[] Publicvalue = Rsapublic.encrypt (Encoding.UTF8.GetBytes (data),false); stringPublicstr = convert.tobase64string (Publicvalue);//convert byte to string using Base64 returnpublicstr;}/// <summary>///using RSA to implement decryption/// </summary>/// <param name= "Data" >Decrypt Data</param>/// <returns></returns> Public stringRsadecrypt (stringdata) { //C # default can only be decrypted with [private key] (want to use [private key encryption] can be implemented using third-party component Bouncycastle) stringPrivatekeypath =@"D:\\privatekey.xml"; stringPrivatekey =File.readalltext (Privatekeypath); //Create an RSA object and load the [private key]RSACryptoServiceProvider rsaprivate =NewRSACryptoServiceProvider (); Rsaprivate.fromxmlstring (Privatekey); //Decrypt the data byte[] Privatevalue = Rsaprivate.decrypt (convert.frombase64string (data),false);//use Base64 to convert a string to byte stringPrivatestr =Encoding.UTF8.GetString (Privatevalue); returnprivatestr;}
Appendix:
Online encryption and decryption site: Http://web.chacuo.net/netrsakeypair
C # Implementation of asymmetric encryption RSA