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