1. net source code:
Using system; using system. text; using system. security. cryptography; namespace consoleapplicationdemo {// <summary> // AES symmetric encryption and decryption class // </Summary> public class aeshelper {# Region member variable /// <summary> // /Key (32-bit, otherwise, add 0) // </Summary> private const string _ passwd = "ihlih * 0037joht *) (pijy * () Ji ^) Io % "; /// <summary> /// operation mode /// </Summary> Private Static ciphermode _ ciphermode = ciphermode. ECB; /// <summary> /// fill mode /// </Summary> Private Static paddingmode _ paddingmode = paddingmode. pkcs7; // <summary> // the encoding of the string /// </Summary> Private Static encoding _ encoding = encoding. utf8; # endregion # auxiliary method of region /// <summary> /// obtain 32byte key data /// </Summary> /// <Param name = "password"> password </ param> // <returns> </returns> Private Static byte [] getkeyarray (string password) {If (Password = NULL) {Password = string. empty;} If (password. length <32) {Password = password. padright (32, '0');} else if (password. length> 32) {Password = password. substring (0, 32);} return _ encoding. getbytes (password );} /// <summary> /// convert the character array to a string /// </Summary> /// <Param name = "inputdata"> </param> /// <returns> </returns> Private Static string convertbytetostring (byte [] inputdata) {stringbuilder sb = new stringbuilder (inputdata. length * 2); foreach (var B in inputdata) {sb. append (B. tostring ("X2");} return sb. tostring ();} /// <summary> /// convert the string to a character array /// </Summary> /// <Param name = "inputstring"> </param> /// <returns> </returns> Private Static byte [] convertstringtobyte (string inputstring) {If (inputstring = NULL | inputstring. length <2) {Throw new argumentexception ();} int L = inputstring. length/2; byte [] result = new byte [l]; for (INT I = 0; I <L; ++ I) {result [I] = convert. tobyte (inputstring. substring (2 * I, 2), 16);} return result ;} # endregion # region encryption // <summary> // encrypt byte data // </Summary> // <Param name = "inputdata"> the byte data to be encrypted </param> /// <Param name = "password"> password </param> /// <returns> </returns> Public static byte [] encrypt (byte [] inputdata, string password) {aescryptoserviceprovider AES = new aescryptoserviceprovider (); AES. key = getkeyarray (password); AES. mode = _ ciphermode; AES. padding = _ paddingmode; icryptotransform transform = AES. createencryptor (); byte [] DATA = transform. transformfinalblock (inputdata, 0, inputdata. length); AES. clear (); return data ;}/// <summary> // encrypted string (encrypted as a hexadecimal string) /// </Summary> /// <Param name = "inputstring"> string to be encrypted </param> /// <Param name = "password"> password </ param> // <returns> </returns> Public static string encrypt (string inputstring, string password) {byte [] toencryptarray = _ encoding. getbytes (inputstring); byte [] result = encrypt (toencryptarray, password); Return convertbytetostring (result );} /// <summary> /// string encryption (encrypted as a hexadecimal string) /// </Summary> /// <Param name = "inputstring"> the string to be encrypted </param> /// <returns> the encrypted string </returns> public static string encryptstring (string inputstring) {return encrypt (inputstring, _ passwd );} # endregion # region decryption /// <summary> // decrypt the byte array /// </Summary> /// <Param name = "inputdata"> the byte data to be decrypted </param> /// <Param name = "password"> password </param> /// <returns> </returns> Public static byte [] decrypt (byte [] inputdata, string password) {aescryptoserviceprovider AES = new aescryptoserviceprovider (); AES. key = getkeyarray (password); AES. mode = _ ciphermode; AES. padding = _ paddingmode; icryptotransform transform = AES. createdecryptor (); byte [] DATA = NULL; try {DATA = transform. transformfinalblock (inputdata, 0, inputdata. length);} catch {return NULL;} AES. clear (); return data ;} /// <summary> /// decrypt the hexadecimal string as a string // </Summary> /// <Param name = "inputstring"> string to be decrypted </param> /// <Param name = "password"> password </param> /// <returns> string </returns> Public static string decrypt (string inputstring, string password) {byte [] todecryptarray = convertstringtobyte (inputstring); string decryptstring = _ encoding. getstring (decrypt (todecryptarray, password); Return decryptstring ;} /// <summary> /// decrypt the hexadecimal string as a string /// </Summary> /// <Param name = "inputstring"> string to be decrypted </param> // <returns> decrypted string </returns> Public static string decryptstring (string inputstring) {return decrypt (inputstring, _ passwd) ;}# endregion }}
2. Android code:
Package COM. google. test; import Java. io. unsupportedencodingexception; import javax. crypto. cipher; import javax. crypto. spec. secretkeyspec; /** AES symmetric encryption/Decryption class **/public class aeshelper {/** algorithm/mode/fill **/Private Static final string ciphermode = "AEs/ECB/pkcs5padding "; /** create key **/Private Static secretkeyspec createkey (string password) {byte [] DATA = NULL; If (Password = NULL) {Password = "";} stringbuffer sb = new stringbuffer (32); sb. append (password); While (sb. length () <32) {sb. append ("0");} If (sb. length ()> 32) {sb. setlength (32);} Try {DATA = sb. tostring (). getbytes ("UTF-8");} catch (unsupportedencodingexception e) {e. printstacktrace ();} return New secretkeyspec (data, "AES");}/** encrypt byte data **/public static byte [] encrypt (byte [] content, string password) {try {secretkeyspec key = createkey (password); cipher = cipher. getinstance (ciphermode); cipher. init (cipher. encrypt_mode, key); byte [] result = cipher. dofinal (content); return result;} catch (exception e) {e. printstacktrace ();} return NULL;}/** encrypted (the result is a hexadecimal string) **/public static string encrypt (string content, string password) {byte [] DATA = NULL; try {DATA = content. getbytes ("UTF-8");} catch (exception e) {e. printstacktrace ();} DATA = encrypt (data, password); string result = byte2hex (data); return result ;} /** decrypt the byte array **/public static byte [] decrypt (byte [] content, string password) {try {secretkeyspec key = createkey (password); cipher = cipher. getinstance (ciphermode); cipher. init (cipher. decrypt_mode, key); byte [] result = cipher. dofinal (content); return result;} catch (exception e) {e. printstacktrace ();} return NULL;}/** decrypt the hexadecimal string as the string **/public static string decrypt (string content, string password) {byte [] DATA = NULL; try {DATA = hex2byte (content);} catch (exception e) {e. printstacktrace ();} DATA = decrypt (data, password); If (Data = NULL) return NULL; string result = NULL; try {result = new string (data, "UTF-8");} catch (unsupportedencodingexception e) {e. printstacktrace ();} return result;}/** convert the byte array to a hexadecimal string **/public static string byte2hex (byte [] B) {// The number of one byte, stringbuffer sb = new stringbuffer (B. length * 2); string TMP = ""; for (INT n = 0; n <B. length; n ++) {// The integer is converted to hexadecimal to indicate TMP = (Java. lang. integer. tohexstring (B [N] & 0xff); If (TMP. length () = 1) {sb. append ("0");} sb. append (TMP);} return sb. tostring (). touppercase (); // convert to uppercase}/** convert the hex string to a byte array **/Private Static byte [] hex2byte (string inputstring) {If (inputstring = NULL | inputstring. length () <2) {return New byte [0];} inputstring = inputstring. tolowercase (); int L = inputstring. length ()/2; byte [] result = new byte [l]; for (INT I = 0; I <L; ++ I) {string TMP = inputstring. substring (2 * I, 2 * I + 2); Result [I] = (byte) (integer. parseint (TMP, 16) & 0xff);} return result ;}}
In addition, to support AEs/ECB/zerobytepadding (corresponding to. Net paddingmode. zeros), you need to introduce bcprov. Jar!