AES and BASE64 Encryption

Source: Internet
Author: User

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

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.