AES encryption and decryption in C #

Source: Internet
Author: User

///AES Encryption/// </summary>        /// <param name= "Inputdata" >the data entered</param>        /// <param name= "IV" >Vector 128 bit</param>        /// <param name= "Strkey" >encryption Key</param>        /// <returns></returns>         Public Static byte[] Aesencrypt (byte[] Inputdata,byte[] IV,stringstrkey) {            //Packet Encryption AlgorithmSymmetricAlgorithm des =rijndael.create (); byte[] Inputbytearray = Inputdata;//get an array of bytes that need to be encrypted//set key and key vectorsDes. Key = Encoding.UTF8.GetBytes (strkey.substring (0, +)); Des.iv=IV; using(MemoryStream ms =NewMemoryStream ()) {                using(CryptoStream cs =NewCryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write)) {cs. Write (Inputbytearray,0, inputbytearray.length); Cs.                    FlushFinalBlock (); byte[] Cipherbytes = Ms. ToArray ();//an array of bytes that have been encryptedcs.                    Close (); Ms.                                       Close (); returncipherbytes; }            }        }        /// <summary>        ///AES Decryption/// </summary>        /// <param name= "Inputdata" >the data entered</param>        /// <param name= "IV" >Vector</param>        /// <param name= "Strkey" >Key</param>        /// <returns></returns>         Public Static byte[] Aesdecrypt (byte[] Inputdata,byte[] IV,stringstrkey) {SymmetricAlgorithm des=rijndael.create (); Des. Key= Encoding.UTF8.GetBytes (Strkey.substring (0, +)); Des.iv=IV; byte[] Decryptbytes =New byte[Inputdata.            Length]; using(MemoryStream ms =NewMemoryStream (Inputdata)) {                using(CryptoStream cs =NewCryptoStream (MS, Des. CreateDecryptor (), CryptoStreamMode.Read)) {cs. Read (Decryptbytes,0, decryptbytes.length); Cs.                    Close (); Ms.                Close (); }            }            returndecryptbytes; }

AES encryption and decryption in C #

Related Article

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.