C # encryption and decryption using AES,

Source: Internet
Author: User

C # encryption and decryption using AES,

Many tutorials on the Internet use the Rijndael class for encryption and decryption, but this class is not supported by the author. Is it because special references are required?

 

However, the author finds the encryption and decryption method by referring to the official documentation, that is, using the AES class, with the same effect

 

The code is described first:

/// <Summary> // AES encryption // </summary> /// <param name = "plainBytes"> encrypted plaintext </param> /// <param name = "bKey"> key </param> // <param name = "bVector"> vector </param> // <returns> plaintext </returns> private static byte [] AESEncrypt (byte [] data, byte [] bKey, byte [] bVector) {Aes aesAlg = Aes. create (); aesAlg. key = bKey; aesAlg. IV = bVector; aesAlg. padding = PaddingMode. zeros; ICryptoTransform encryptor = aesAlg. createEncryptor (aesAlg. key, aesAlg. IV); return encryptor. transformFinalBlock (data, 0, data. length );}

This is a function used for encryption.

Parameters:

Data array, which is the byte array to be encrypted. If it is an operation file, it is recommended to call the function cyclically. Each operation should not be too large, so that the progress can be displayed and the file will not be too large to consume too much memory.

Note that, if the length of an array is not an integer multiple of 16, the encrypted array is automatically followed by an integer multiple of the byte to 16 (the reason is described in the decryption section)

BKey array, which is the password used for encryption. Note that the password must be 128 or 256 characters. In fact, 112 bits are acceptable, but they are not universal. 128 or 256 is recommended.

If the password is 128 bits, the encryption method is automatically adjusted to AES128. The same applies to 256 bits. If the length is not specified, an exception occurs.

BVector array. If you know about the Aes encryption mechanism, you will know that this is the initial vector of encryption.

Many people do not understand why this parameter is required. Isn't that an option? No, this is useful!

For example, if you encrypt a string and the password is the same, the result is OK. This will reduce security, and others may find the original information through comparison.

However, if you change the initial vector, that is, the password used for each round of AES encryption is different from the previous one. In this way, the encrypted results are different.

According to my understanding, the initial vector is equivalent to the second set of AES passwords.

Return Value:

There is nothing to say, that is, encrypted data.

 

Next let's take a look at the decryption operation.

/// <Summary> // AES decryption // </summary> /// <param name = "encryptedBytes"> decrypted ciphertext </param> /// <param name = "bKey"> key </param> // <param name = "bVector"> vector </param> // <returns> plaintext </returns> private static byte [] AESDecrypt (byte [] data, byte [] bKey, byte [] bVector) {Aes aesAlg = Aes. create (); aesAlg. key = bKey; aesAlg. IV = bVector; aesAlg. padding = PaddingMode. zeros; ICryptoTransform decryptor = aesAlg. createDecryptor (aesAlg. key, aesAlg. IV); return decryptor. transformFinalBlock (data, 0, data. length );}

The parameters are easy to understand. They are the same as those used for encryption. The returned value is the decrypted data.

But here are some notes:

As mentioned above, if the length of the array used for encryption is not an integer multiple of 16, the length is automatically added.

This is because the length of the decrypted data parameter must be an integer multiple of 16. The encrypted result is a data with a length automatically added. The data can be directly used as a parameter of the decryption function.

 

I found that this class can be used in the console, WPF, WinForm, and UWP, and is easier to use than Rijndael.

 

------------------------------------------- Manual demarcation line --------------------------------------------

 

If you want to encrypt the string, you only need to call these two functions and perform point conversion.

/// <Summary> /// encryption /// </summary> /// <param name = "data"> </param> /// <returns> </ returns> public static string AESEncrypt (string data) {return Encoding. unicode. getString (AESEncrypt (Encoding. unicode. getBytes (data )));} /// <summary> /// decrypt /// </summary> /// <param name = "data"> </param> /// <returns> </ returns> public static string AESDecrypt (string data) {return Encoding. unicode. getString (AESDecrypt (Encoding. unicode. getBytes (data )));}

Is it very simple?

 

If you want to encrypt the file, you only need to call these two functions and perform point conversion (the premise is that a small file, if it is a large file, must be called multiple times in a loop, otherwise, the program may crash if the memory consumption is too high)

/// <Summary> /// encrypt the file /// </summary> /// <param name = "data"> </param> /// <returns> </returns> public static void AESEncryptFile (string path) {File. writeAllBytes (path, AESEncrypt (File. readAllBytes (path )));} /// <summary> /// decrypt the file /// </summary> /// <param name = "data"> </param> /// <returns> </returns> public static void AESDecryptFile (string path) {File. writeAllBytes (path, AESDecrypt (File. readAllBytes (path )));}

 

 

If you have any questions, please contact wwh8797@qq.com

 

The UWP application "encryption and decryption" mainly uses a similar method. Of course, "encryption and decryption" can also be used to decrypt large files and support batch operations. This requires multiple cycles and calls the encryption and decryption function.

There is also the UWP app "Initial heart Diary", which is also used to encrypt users' private data. Aes encryption technology is almost unknown at this stage. Therefore, the "initial care Diary" can better protect user privacy

Readers can refer to the two software on their own.

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.