C # Use des for encryption

Source: Internet
Author: User
Tags asymmetric encryption

The des (Data Encryption Standard) algorithm is a symmetric cipher system in the cryptographic system and has become the American Data Encryption Standard. It is a symmetric encryption algorithm developed by IBM in 1972.

There is not much to say about the theory. It is easy to get a headache. We only need to have two concepts: symmetric encryption and asymmetric encryption.

Symmetric encryption: only one key is used to encrypt and decrypt data. for example, like the locks we usually use to open a door, a key is used to close the door. the key is equivalent to the key. in the code, it is equivalent to a parameter passed to the function.

Asymmetric encryption: There are two keys, one public key and one private key. This is equivalent to locking the door with one key. When opening the door, you need to use another key.

 

For symmetric encryption, we may be easy to understand. If you only need a key, you will get a variable when writing code. Then, the encrypted function uses this variable as a parameter, and the decryption function uses it as a parameter.

Asymmetric encryption is a little hard to understandIt is not generally used in the same program. it is used to transmit data between different programs. in such a scenario, you enter a password on a website and then upload it to the server for verification. the password is not safe during transmission. If it is intercepted, you will want to encrypt it. in this way, people cannot understand the interception. at this time, you cannot use symmetric encryption, because it is just a key that your client program knows and uses it for encryption, but the server does not know it. When you think of the transfer password, the key will also be passed over, in this way, it is equally insecure and may be intercepted. therefore, asymmetric encryption was unveiled.

First, the server endGenerate a key pair using an algorithm, A public key a, and a private key B. In this way, things encrypted by A can be decrypted by B, and things encrypted by B can be decrypted by.. The so-called Public Key is made public, and everyone knows it. in this way, everyone can use the public key to encrypt the private key. As long as the server knows it, others will not know it, so you are safe. someone will ask if I can use public key a to calculate B. In theory, it is natural, otherwise there will be no password cracking. it may be difficult. this is mainly because some mathematical algorithms are not easy to reverse push. This involves some mathematical theories.

 

We don't care about the specific implementation of the algorithm. Some system functions of. Net have done that for us. We just need to call it.

Using system. text;

Using system. IO;

Using system. Security. cryptography; // to call those functions, you must first reference these two namespaces.

 

String mykey = "weiwenhp"; // This is the legendary key. the des key is a 64-bit binary, which can only be 8 characters in string format.

// Encryption function

String jiami (string sourcestring, string key)

{

Byte [] keybytes = encoding. utf8.getbytes (key );

Byte [] keyiv = keybytes;

Byte [] inputbytearray = encoding. utf8.getbytes (sourcestring );

Descryptoserviceprovider desprovider = new descryptoserviceprovider ();

Memorystream memstream = new memorystream ();

Cryptostream crypstream = new cryptostream (memstream, desprovider. createencryptor (keybytes, keyiv), cryptostreammode. Write );

Crypstream. Write (inputbytearray, 0, inputbytearray. Length );

Crypstream. flushfinalblock ();

Returnconvert. tobase64string (memstream. toarray ());

}

 

// Decrypted Function

String jiemi (string encryptstring, string key)

{

Byte [] keybytes = encoding. utf8.getbytes (key );

Byte [] keyiv = keybytes;

Byte [] inputbytearray = convert. frombase64string (encryptstring );

Descryptoserviceprovider desprovider = new descryptoserviceprovider ();

Memorystream memstream = new memorystream ();

Cryptostream crypstream = new cryptostream (memstream, desprovider. createdecryptor (keybytes, keyiv), cryptostreammode. Write );

Crypstream. Write (inputbytearray, 0, inputbytearray. Length );

Crypstream. flushfinalblock ();

Returnencoding. utf8.getstring (memstream. toarray ());

}

 

String Password = "PWD"; // assume this is your password

String miwen = jiami (password, mykey); // encrypted ciphertext. The value here is 9tesi4kkhxw =

String mingwen = jiemi (miwen, mykey); // decrypt the ciphertext. The value here is PWD.

 

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.