Yesterday the boss asked me to look at the Android encryption algorithm. So I looked for it online and found the AES encryption algorithm. (Of course, there are md5,base64 what http://snowolf.iteye.com/blog/379860 this article lists a lot, but basically is J2SE platform, Android platform does not necessarily support, But the AES algorithm Android comes with its own package, which can be seen from the official http://developer.android.com/reference/javax/crypto/Cipher.html.
)
What is the AES encryption algorithm? We can go to Google on their own, expert-level program ape to write a good package, project staff will be able to use.
This example is actually from http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml.
src folder main file:
Package Com.qq;import Java.security.securerandom;import Javax.crypto.cipher;import javax.crypto.KeyGenerator; Import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;import Android.app.activity;import Android.os.bundle;import Android.util.log;public class Simplecrypto extends Activity {public static String encrypt ( String seed, String cleartext) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); Byte[] result = Encrypt (Rawkey, cleartext.getbytes ()); return Tohex (Result); public static string decrypt (string seed, string encrypted) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); byte[] enc = tobyte (encrypted); Byte[] result = Decrypt (rawkey, ENC); return new String (result); } private static byte[] Getrawkey (byte[] seed) throws Exception {Keygenerator KGen = keygenerator.ge Tinstance ("AES"); securerandom sr = SecUrerandom.getinstance ("sha1prng"); Sr.setseed (seed); Kgen.init (+, SR); 192 and available Secretkey Skey = Kgen.generatekey (); Byte[] raw = skey.getencoded (); return raw; } private static byte[] Encrypt (byte[] raw, byte[] clear) throws Exception {Secretkeyspec SK Eyspec = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.encrypt_mode, Skeyspec); Byte[] Encrypted = cipher.dofinal (clear); return encrypted; } private static byte[] Decrypt (byte[] raw, byte[] encrypted) throws Exception {Secretkeyspec SKEYSP EC = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, Skeyspec); byte[] decrypted = cipher.dofinal (encrypted); return decrypted; } public StatIC string Tohex (String txt) {return Tohex (Txt.getbytes ()); public static string Fromhex (String hex) {return new string (ToByte (hex)); } public static byte[] ToByte (String hexstring) {int len = Hexstring.length ()/2; Byte[] result = new Byte[len]; for (int i = 0; i < len; i++) Result[i] = integer.valueof (hexstring.substring (2*i, 2*i+2), +). Bytevalue (); return result; public static String Tohex (byte[] buf) {if (buf = = null) return ""; StringBuffer result = new StringBuffer (2*buf.length); for (int i = 0; i < buf.length; i++) {Appendhex (result, buf[i]); } return result.tostring (); } private Final static String HEX = "0123456789ABCDEF"; private static void Appendhex (StringBuffer sb, byte b) {Sb.append (Hex.charat (b>>4) &0x0f). Append (Hex.charat (b&0x0f)); }/** called when the activity is first created. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); String Masterpassword = "a"; String originaltext = "0123456789"; byte[] Text = new byte[]{' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '}; byte[] Password = new byte[]{' A '}; try {String Encryptingcode = Simplecrypto.encrypt (Masterpassword,originaltext);//SYSTEM.OUT.PRINTLN ("encrypted result is" + enc Ryptingcode); LOG.I ("Encrypted result is", encryptingcode); String Decryptingcode = Simplecrypto.decrypt (Masterpassword, Encryptingcode); System.out.println ("decryption result is" + decryptingcode); LOG.I ("decryption result", decryptingcode); } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } }}
Layout file main.xml and configuration file androidmanifest.xml by default. The final result in log look, using adb logcat > D:\1.txt to locate the 1.txt file D, and then open with Notepad, look for "encrypted results for" can see:
Execution results (as seen from the log log):
i/encryption result is (): Bfb77d8f1e1ee9d5e252926a12659de8
i/decryption Results (190): 0123456789