public class Aeshelper {//<summary>////</summary> Private STA Tic string Key {get {return ' abcdef1234567890 '; Must be 16-bit}}
//default key vector private static byte[] _key1 = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
//<summary> // AES Encryption and Base64 encoding processing ///</summary> //<param name= "plaintext" > Clear text string </param> // <returns> convert encrypted ciphertext to BASE64 encoding to display </returns> public static String Aesencrypt (string plaintext) { //Packet encryption algorithm SymmetricAlgorithm des = rijndael.create (); byte[] Inputbytearray = Encoding.UTF8.GetBytes ( plaintext);//Get the byte array to be encrypted //Set key and key vector Des. Key = Encoding.UTF8.GetBytes (key); des.iv = _key1; byte[] cipherbytes = null; using (MemoryStream ms = new MemoryStream ()) { using (CryptoStream cs = new CryptoStream (MS, Des. CreateEncryptor (), CryptoStreamMode.Write) { cs. Write (Inputbytearray, 0, inputbytearray.length); CS. FlushFinalBlock (); cipherBytes = Ms. ToArray ();//Get encrypted byte array cs. Close (); Ms. Close (); } } return convert.tobase64string (cipherbytes); }
//<summary> //AES decryption and Base64 encoding processing //</summary> //<param name= "ciphertext" > Ciphertext string </param> ///<returns> returns the decrypted plaintext string </returns> public static string Aesdecrypt (String showtext) { byte[] ciphertext = convert.frombase64string (showtext );
symmetricalgorithm des = rijndael.create (); des. Key = Encoding.UTF8.GetBytes (key); des.iv = _key1; byte[] decryptbytes = new Byte[ciphertext.length ]; using (MemoryStream ms = new MemoryStream ( Ciphertext)) { using (CryptoStream cs = new CryptoStream (MS, Des. CreateDecryptor (), CryptoStreamMode.Read) { Cs. Read (decryptbytes, 0, decryptbytes.length); CS. Close (); Ms. Close (); } } return Encoding.UTF8.GetString (decryptbytes). Replace ("\", ""); ////rear The string to '/' }
}
AES and BASE64 Encryption