C#aes encryption

Source: Internet
Author: User

Write an interface today that requires the corresponding AES encryption (128-bit), example PHP

C # implementation

/// <summary>        ///AES Encrypted output hex format/// </summary>        /// <param name= "str" >plaintext (pending encryption)</param>        /// <param name= "key" >Ciphertext</param>        /// <returns></returns>         Public Static stringAesencrypt (stringStrstringkey) {            if(string. IsNullOrEmpty (str))return NULL; Byte[] Toencryptarray=Encoding.UTF8.GetBytes (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.Security.Cryptography.RijndaelManaged {Key=Encoding.UTF8.GetBytes (key), Mode=System.Security.Cryptography.CipherMode.ECB, Padding=System.Security.Cryptography.PaddingMode.Zeros//Encrypt what format corresponds to the line, the basic words should be PKCS7}; System.Security.Cryptography.ICryptoTransform Ctransform=RM.            CreateEncryptor (); Byte[] Resultarray= Ctransform.transformfinalblock (Toencryptarray,0, toencryptarray.length); //return convert.tobase64string (resultarray, 0, resultarray.length);            returnbytearraytohexstring (resultarray); }    /// <summary>    ///converts a byte array to a formatted 16 binary string/// </summary>    /// <param name= "Data" >byte array</param>    /// <returns>formatted 16 binary string</returns>     Public Static stringBytearraytohexstring (byte[] data) {StringBuilder SB=NewStringBuilder (data. Length *3); foreach(byteBinchdata) {            //16 binary digitsSb. Append (convert.tostring (b, -). PadLeft (2,'0')); //16 binary digits separated by a space//sb. Append (convert.tostring (b, 16). PadLeft (2, ' 0 '). PadRight (3, "));        }        returnsb. ToString ().    ToUpper (); }
     /// <summary>        ///AES decrypted output hex format/// </summary>        /// <param name= "str" >plaintext (to be decrypted)</param>        /// <param name= "key" >Ciphertext</param>        /// <returns></returns>         Public Static stringAesdecrypt (stringStrstringkey) {            if(string. IsNullOrEmpty (str))return NULL; //byte[] Toencryptarray = convert.frombase64string (str);            byte[] Toencryptarray =Hexstringtobytearray (str); System.Security.Cryptography.RijndaelManaged RM=NewSystem.Security.Cryptography.RijndaelManaged {Key=Encoding.UTF8.GetBytes (key), Mode=System.Security.Cryptography.CipherMode.ECB, Padding=System.Security.Cryptography.PaddingMode.Zeros}; System.Security.Cryptography.ICryptoTransform Ctransform=RM.            CreateDecryptor (); Byte[] Resultarray= Ctransform.transformfinalblock (Toencryptarray,0, toencryptarray.length); returnEncoding.UTF8.GetString (resultarray); }                  /// <summary>    ///converts the specified 16 binary string to a byte array/// </summary>    /// <param name= "S" >16 binary strings (such as: "7F 2C 4 A" or "7f2c4a" can be)</param>    /// <returns>byte array corresponding to the 16 binary string</returns>     Public Static byte[] Hexstringtobytearray (strings) {s= S.replace (" ",""); byte[] buffer =New byte[S.length/2];  for(inti =0; i < s.length; i + =2) Buffer[i/2] = (byte) Convert.tobyte (s.substring (i,2), -); returnbuffer; }

The output is Base64 will cancel my comment, this is because the demand for the hex format

C#aes encryption

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.