C # Implementation of AES encryption and decryption complete instance _c# tutorial

Source: Internet
Author: User
Tags pkcs7

The examples in this article describe the AES cryptographic decryption implemented by C #. Share to everyone for your reference, specific as follows:

/****************************************************************** * Creator: HTL * Description: C # AES Encryption decryption **********************
/using System;
Using System.Security.Cryptography;
Using System.Text;
Using System.IO;
 public class Test {public static void Main () {//password string password= "1234567890123456";
 Cryptographic initialization vector string iv= "";
 String Message=aesencrypt ("abcdefghigklmnopqrstuvwxyz0123456789", Password,iv);
 Console.WriteLine (message);
 Message=aesdecrypt ("8z3dzzqn05fmiublowexk0cabs4ty2gorc2ddpvlsn/tp+vujgepqimv1usaverr", Password,iv);
 Console.WriteLine (message); }///<summary>///AES Encryption///</summary>///<param name= "text" > Encryption character </param>///<param na Me= "password" > encrypted password </param>///<param name= "IV" > key </param>///<returns></returns > public static string Aesencrypt (string text, string password, string iv) {RijndaelManaged rijndaelcipher = new Ri
 Jndaelmanaged (); RijndaelciPher.
 Mode = CIPHERMODE.CBC;
 rijndaelcipher.padding = PADDINGMODE.PKCS7;
 rijndaelcipher.keysize = 128;
 rijndaelcipher.blocksize = 128;
 byte[] pwdbytes = System.Text.Encoding.UTF8.GetBytes (password);
 byte[] keybytes = new BYTE[16];
 int len = pwdbytes.length;
 if (len > keybytes.length) len = keybytes.length;
 System.Array.Copy (Pwdbytes, Keybytes, Len);
 Rijndaelcipher.key = keybytes;
 byte[] Ivbytes = System.Text.Encoding.UTF8.GetBytes (iv);
 RIJNDAELCIPHER.IV = new BYTE[16];
 ICryptoTransform transform = Rijndaelcipher.createencryptor ();
 byte[] plaintext = Encoding.UTF8.GetBytes (text); Byte[] Cipherbytes = transform.
 TransformFinalBlock (plaintext, 0, plaintext.length);
 Return convert.tobase64string (cipherbytes); ///<summary>///AES decryption///</summary>///<param name= "text" ></param>///<param name= " Password "></param>///<param name=" IV "></param>///<returns></returns> Public static string Aesdecrypt (StrinG-Text, string password, string iv) {RijndaelManaged rijndaelcipher = new RijndaelManaged ();
 Rijndaelcipher.mode = CIPHERMODE.CBC;
 rijndaelcipher.padding = PADDINGMODE.PKCS7;
 rijndaelcipher.keysize = 128;
 rijndaelcipher.blocksize = 128;
 byte[] EncryptedData = convert.frombase64string (text);
 byte[] pwdbytes = System.Text.Encoding.UTF8.GetBytes (password);
 byte[] keybytes = new BYTE[16];
 int len = pwdbytes.length;
 if (len > keybytes.length) len = keybytes.length;
 System.Array.Copy (Pwdbytes, Keybytes, Len);
 Rijndaelcipher.key = keybytes;
 byte[] Ivbytes = System.Text.Encoding.UTF8.GetBytes (iv);
 RIJNDAELCIPHER.IV = ivbytes;
 ICryptoTransform transform = Rijndaelcipher.createdecryptor (); Byte[] plaintext = transform.
 TransformFinalBlock (EncryptedData, 0, encrypteddata.length);
 return Encoding.UTF8.GetString (plaintext);

 }
}

PS: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:

Tools.jb51.net/password/my_password_safe

High Strength Password Generator:

Tools.jb51.net/password/createstrongpassword

MD5 Online Encryption Tool:

Tools.jb51.net/password/createmd5password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:

Tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:

Tools.jb51.net/password/hash_encrypt

More about C # related content can also view the site topics: "C # Encryption and decryption algorithms and techniques Summary", "C # form Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Usage Skills summary", "C # operation Excel Skills Summary "," C # in the XML file Operation skills Summary, "C # Data structure and algorithm tutorial", "C # array Operation skills Summary" and "C # object-oriented Program Design Introductory Course"

I hope this article will help you with your C # programming.

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.