C # Programming Summary (VII) Data encryption

Source: Internet
Author: User
Tags hash md5 md5 encryption sha1 sha1 encryption sha1 hash asymmetric encryption

Overview

The basic process of data encryption is to deal with a certain algorithm for files or data that were originally plaintext. Make it unreadable a piece of code, usually called "ciphertext", so that it can only enter the appropriate key to display the original content, through such a way to protect the data is not stolen by the illegal people, read the purpose. The reverse process of the process is the process of decrypting and converting the encoded information into its original data. Encryption is based on the mathematical encoding and decoding of the information. There are two kinds of encryption types, symmetric and asymmetric encryption, symmetric encryption both sides adopt a common key. Asymmetric encryption, which has two keys, one is public key (external disclosure), one is private key (external secrecy).

First, the digest algorithm

Data digest algorithm is a very important branch of cryptography, which can be used to encrypt sensitive information by extracting fingerprint information from all data to realize data signature and data integrity check. The Data digest algorithm is also known as a hash algorithm or hashing algorithm.

In a strict sense, the digest algorithm is not a cryptographic algorithm, but in a specific application similar to the use of cryptographic algorithms, or with the encryption algorithm used, here also introduced.

Application scope: Password encryption, data integrity check, digital signature, etc.

This article introduces two commonly used abstract algorithms, MD5 and SHA1.

Hint: The current MD5 has been cracked, recommended to use SHA1

1, MD5

A hash function maps a binary string of any length to a small binary string of fixed length. The cryptographic hash function has the property that it is not possible to find two different inputs with the same value in the calculation, that is, the hash value of the two sets of data matches only if the corresponding data matches. A small amount of change in the data produces unpredictable and significant changes in the hash value. The hash value of the MD5 algorithm is 128 bits in size.

The ComputeHash method of the MD5 class returns the hash as an array of 16 bytes. Note that some MD5 implementations generate a 32-character hexadecimal format hash. To interoperate with this class implementation, format the return value of the ComputeHash method as a hexadecimal value.

MD5 Encryption:

<summary>
        ///MD5 encrypted to 32-character-length 16-string
        ///</summary>
        ///<param name= "Input" ></ param>
        ///<returns></returns> public
        static string EncryptByMD5 (string input)
        {
            MD5 Md5hasher = MD5. Create ();
            byte[] data = Md5hasher.computehash (Encoding.UTF8.GetBytes (input));
    
            StringBuilder Sbuilder = new StringBuilder ();
            Converts each byte into 16 for
            (int i = 0; i < data. Length; i++)
            {
                sbuilder.append (Data[i]. ToString ("X2"));
            }
    
            return sbuilder.tostring ();            
        }

2, SHA1

Calculates the SHA1 hash value of the input data.

A hash value is used as a unique value that represents a fixed size for large amounts of data. If the corresponding data also matches, the hashes of the two datasets should match. A small amount of change in the data produces unpredictable and significant changes in the hash value.

The hash value of the SHA1 algorithm is 160 bits in size.

SHA1 Encryption:

<summary>
        ///SHA1 encryption
        ///</summary>
        ///<param name= "input" ></param>
        // /<returns></returns> public
        static string EncryptBySHA1 (string input)
        {
            SHA1 sha = new SHA1CryptoServiceProvider ();          
            byte[] bytes = Encoding.Unicode.GetBytes (input);
            Byte[] result = Sha.computehash (bytes);
            return bitconverter.tostring (result);
        }

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.