Android AES encryption tool and androidaes Tool
1. AES encryption tool
Java does not support PKCS7Padding. Only PKCS5Padding is supported. We know that the encryption algorithm consists of algorithm + mode + fill. This article uses the PKCS5Padding encryption method.
''
Package com. example. aesdemo; import java. io. unsupportedEncodingException; import javax. crypto. cipher; import javax. crypto. spec. secretKeySpec; /// ** AES symmetric encryption and 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); System. out. println (key); Cipher 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 = 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 a 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) {// number of bytes, 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 ;}}
Use ~~~~~~~~~~~~~~~~ ··
String masterPassword = "a"; String originalText = "on"; try {String encryptingCode = AESHelper. encrypt (originalText, masterPassword); // System. out. println ("encrypted Result:" + encryptingCode); Log. I ("the encryption result is", encryptingCode); String decryptingCode = AESHelper. decrypt (encryptingCode, masterPassword); // System. out. println ("decryption Result:" + decryptingCode); Log. I ("decryption result", decryptingCode);} catch (Exception e) {// TODO Auto-generated catch block e. printStackTrace ();}}
Result
The encrypted result is (
707
): E55C24701F6380478E1940ADDFD08D22
Decryption result (
707
): Yu