C # Analysis of Common encryption methods

Source: Internet
Author: User
Tags asymmetric encryption

In this article, I use examples to explain the common encryption algorithms of C.

I. MD5 Encryption Algorithm

I think this is an algorithm that everyone has heard of and may be used more often. So what is the MD5 algorithm? The full name of MD5 is message-digest algorithm 5. In short, it is unidirectional encryption, that is, the plaintext cannot be exported Based on the ciphertext.

MD5:

1. Generate a summary of a piece of information. The summary is unique and can be used as a digital signature.

2. used to verify the validity of the file (whether there is lost or damaged data ),

3. encryption of user passwords,

4. Calculate the hash value in the hash function

From the above main purposes, we can see that, due to some irreversible features of the algorithm, it provides better security for encryption applications. By using the MD5 encryption algorithm, a 128-bit integer is generated when a byte string of any length is input. Therefore, MD5 is widely used for password encryption. The following is an example of how to encrypt a password.

Let's take a look at the demo:

The Code is as follows:

First, you need to introduce the namespace:

?

Using System. Security;

Using System. Security. Cryptography;

Private void btnmd5_Click (object sender, EventArgs e)

{

MD5 md5 = new MD5CryptoServiceProvider ();

Byte [] palindata = Encoding. Default. GetBytes (txtyuan. Text); // convert the string to be encrypted to a byte array

Byte [] encryptdata = md5.ComputeHash (palindata); // encrypt the string and convert it to a character array

Txtjiami. Text = Convert. ToBase64String (encryptdata); // Convert the encrypted byte array to an encrypted string

}

Here, we need to note that, during the encryption process, the encrypted string must be converted into a byte array before encryption. After encryption, the encrypted byte data must be generated and then converted into a ciphertext.

Ii. RSA Encryption Algorithm

Before talking about the RSA encryption algorithm, we need to understand two terms: symmetric encryption and asymmetric encryption.

Symmetric encryption refers to a message that contains something called a key. The key is used to encrypt the message before the message is sent. After the recipient receives the message, the same key is used for decryption.

Asymmetric encryption is a type of encryption algorithm that uses different keys for encryption and decryption. This type of encryption algorithm usually has two keys, A and B. The ciphertext obtained from key A's encrypted data can only be decrypted by key B, even if key A cannot be decrypted). On the contrary, only key A can decrypt the ciphertext obtained by encrypting data with key B. These two keys are called private keys and public keys respectively. As the name suggests, private keys are private keys that you reserve and cannot be disclosed, while public keys are published to the other side of encryption and decryption operations. For different purposes, the keys used for data encryption are also different. Sometimes, public keys are used for encryption and private keys are used for decryption. Sometimes, private keys are used for encryption and public keys are used for decryption ). Asymmetric encryption indicates the RSA algorithm.

The RSA encryption algorithm follows these two terms. The names of RSA come from the names of the three developers. RSA is currently the most influential public key encryption algorithm. It is mostly used for data encryption and digital signature. Although it has such a great influence, it also has some drawbacks. It is very troublesome to Generate Keys. Due to technical restrictions on the generation of prime numbers, it is difficult to achieve one-time encryption and the group length is too large.

The following example shows how to use RSA to encrypt and decrypt data:

First create a global CspParameters object param

Encryption:

?

Private void btnjm_Click (object sender, EventArgs e)

{

Param = new CspParameters ();

Param. KeyContainerName = "Olive"; // The Name Of The key container. The decryption succeeds only when the encryption and decryption are consistent.

Using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (param ))

{

Byte [] plaindata = Encoding. Default. GetBytes (txtyuan. Text); // convert the string to be encrypted to a byte array

Byte [] encryptdata = rsa. Encrypt (plaindata, false); // convert the encrypted byte data into a new encrypted byte array

Txtjiami. Text = Convert. ToBase64String (encryptdata); // Convert the encrypted byte array to a string

}

}

Decryption:

?

Private void btnjiemi_Click (object sender, EventArgs e)

{

Param = new CspParameters ();

Param. KeyContainerName = "Olive ";

Using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (param ))

{

Byte [] encryptdata = Convert.FromBase64String(this.txt jiami. Text );

Byte [] decryptdata = rsa. Decrypt (encryptdata, false );

Txthjiemi. Text = Encoding. Default. GetString (decryptdata );

}

}

Effect

The following example shows how to generate a public key and a private key by using the RSA encryption algorithm.

?

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();

Using (StreamWriter sw = new StreamWriter (@ "D: \ PublicKey. xml") // generate public keys

{

Sw. WriteLine (rsa. ToXmlString (false ));

}

Using (StreamWriter sw = new StreamWriter (@ "D: \ PrivateKey. xml") // The generated private key also contains the private key)

{

Sw. WriteLine (rsa. ToXmlString (false ));

}

Iii. DES encryption

DES encryption: A 56-bit key and an 8-bit parity bit are used to generate a maximum of 64-bit grouping size. This is an iterative group password, using the technology known as Feistel, where the encrypted text block is divided into two halves. Use the sub-key pair to apply the loop function in half of them, and then perform the "XOR" Operation on the output and the other half; then switch the two half, the process continues, but the last loop is not exchanged. DES uses 16 cycles, exclusive or, replacement, replacement, and shift operations. Take a look at the technical terms. Here is a demo to help you understand it.

First define a global byte array and instantiate a global DESCryptoServiceProvider object

Byte [] buffer;

DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider ();

Encryption:

?

Private void button2_Click (object sender, EventArgs e)

{

MemoryStream MS = new MemoryStream (); // create a memory stream first

CryptoStream cryStream = new CryptoStream (MS, DesCSP. CreateEncryptor (), CryptoStreamMode. Write); // connect the memory stream to the encrypted conversion stream

StreamWriter sw = new StreamWriter (cryStream );

Sw. WriteLine (txtyuan. Text); // write the string to be encrypted into the encrypted conversion stream

Sw. Close ();

CryStream. Close ();

Buffer = ms. ToArray (); // convert encrypted streams into byte arrays.

Txtjiami. Text = Convert. ToBase64String (buffer); // Convert the encrypted byte array to a string

}

Decryption:

?

Private void button#click (object sender, EventArgs e)

{

MemoryStream MS = new MemoryStream (buffer); // Add encrypted bytes to the memory stream

CryptoStream cryStream = new CryptoStream (MS, DesCSP. CreateDecryptor (), CryptoStreamMode. Read); // memory stream connected to the decrypted stream

StreamReader sr = new StreamReader (cryStream );

Txthjiemi. Text = sr. ReadLine (); // read the decryption stream as a string

Sr. Close ();

CryStream. Close ();

Ms. Close ();

}

There are also AES encryption algorithms, but AES encryption is a new encryption algorithm that can be used to protect electronic data. The generated password is an iterative symmetric group password, which uses a cyclic structure to replace and replace input data repeatedly in this loop. Because not many of them are used, no specific demonstration is provided here.

Related Article

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.