Encryption and decryption algorithm one: hash algorithm, symmetric plus decryption

Source: Internet
Author: User
Tags decrypt sha1 asymmetric encryption

. NET, the objects involved in the encryption and decryption operations in the System.Security.Cryptography are under the namespace.
Therefore, the using System.Security.Cryptographyshould be added to the program first.
1, hashing algorithm:

An algorithm used to generate hash values for some data fragments, such as messages or session items. A good hashing algorithm with changes in the input data can change the characteristics of each bit in the result hash value, so hashing is useful for detecting any changes in large information objects such as messages. In addition, a good hashing algorithm makes it possible to construct two independent inputs with the same hash to be implemented in a computational way.

Typical hashing algorithms include MD2, MD4, MD5, and SHA-1. Hash algorithms are also known as hash functions. Hashing algorithm is to fight for a carrot a pit principle.

1.1, MD5:
 // <summary>        ///ways to encrypt strings in a MD5 way/// </summary>        /// <param name= "source" >the string to be encrypted</param>        /// <returns>the encrypted string</returns>         Public Static stringMd5encrypt (stringsource) {            Try{MD5 MD5=NewMD5CryptoServiceProvider (); byte[] result =Md5.computehash (Encoding.Default.GetBytes (source)); stringReturnresult ="";  for(inti =0; I < result. Length; i++) {Returnresult+ = Result[i]. ToString ("x"); }                returnReturnresult; }            Catch(Exception ex) {Throw NewException ("The MD5 method failed to encrypt the string. Error message:"+Ex.            Message); }        }
1.2, SHA1:
 // <summary>        ///ways to encrypt strings in a SHA1 way/// </summary>        /// <param name= "source" >the string to be encrypted</param>        /// <returns>the encrypted string</returns>         Public Static stringSha1encrypt (stringsource) {            Try{SHA1 SHA1=NewSHA1CryptoServiceProvider (); byte[] result =Sha1.computehash (Encoding.Default.GetBytes (source)); stringReturnresult ="";  for(inti =0; I < result. Length; i++) {Returnresult+ = Result[i]. ToString ("x"); }                returnReturnresult; }            Catch(Exception ex) {Throw NewException ("The SHA1 method failed to encrypt the string. Error message:"+Ex.            Message); }        }
2. Symmetric plus decryption:

The sender passes the original text through an encrypted algorithm, encrypts the cipher with the key, sends the ciphertext to the receiver, and the receiver decrypts the ciphertext with this key to get the original text. Since the usual encryption algorithms are public, the key to the original encryption is the key. For this kind of encryption and decryption using the same key algorithm, we call symmetric encryption , symmetric encryption of the representative algorithm is the DES algorithm.

Symmetric plus decryption flaw : Because the encryption and decryption using the same key, then the key is kept at least two places, if the encrypted data to be sent to many people, then there will be many people know the key, greatly increased the risk of key leaks , and the key needs to be passed by the sender to the receiver, so how to ensure the security of the key transfer, it becomes another headache. In order to solve this problem, asymmetric encryption has occurred relative to symmetric encryption.

        /// <summary>        /// des encryption Key         /// </summary>         Public Const string " [Email protected]#$& ";
 // <summary>        ///ways to encrypt strings in des mode/// </summary>        /// <param name= "source" >the string to be encrypted</param>        /// <returns>the encrypted string</returns>         Public Static stringDesencrypt (stringsource) {            byte[] Bykey =NULL; byte[] IV = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF }; Try{Bykey= Encoding.Default.GetBytes (M_encryptkey.substring (0,8)); DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); byte[] Inputbytearray =Encoding.Default.GetBytes (source); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des.                CreateEncryptor (Bykey, IV), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs.                FlushFinalBlock (); returnconvert.tobase64string (Ms.            ToArray ()); }            Catch(Exception ex) {Throw NewException ("The des method failed to encrypt the string. Error message:"+Ex.            Message); }        }
 // <summary>        ///ways to decrypt a string in des mode/// </summary>        /// <param name= "source" >the string to decrypt</param>        /// <returns>the decrypted string</returns>         Public Static stringDesdecrypt (stringsource) {            byte[] Bykey =NULL; byte[] IV = {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF }; byte[] Inputbytearray =NewByte[source.            Length]; Try{Bykey= Encoding.Default.GetBytes (M_encryptkey.substring (0,8)); DESCryptoServiceProvider des=NewDESCryptoServiceProvider (); Inputbytearray=convert.frombase64string (source); MemoryStream Ms=NewMemoryStream (); CryptoStream CS=NewCryptoStream (MS, Des.                CreateDecryptor (Bykey, IV), cryptostreammode.write); Cs. Write (Inputbytearray,0, inputbytearray.length); Cs.                FlushFinalBlock (); returnEncoding.Default.GetString (Ms.            ToArray ()); }            Catch(Exception ex) {Throw NewException ("The des method failed to decrypt the string. Error message:"+Ex.            Message); }        }

Encryption and decryption algorithm one: hash algorithm, symmetric plus decryption

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.