The AES encryption algorithm was found. (Of course, there are md5,base64 what http://snowolf.iteye.com/blog/379860 this article lists a lot, but the basic is J2SE platform, the Android platform does not necessarily support, But the AES algorithm Android comes with a package that 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, expert programmers to write a package, engineers will be able to use the line.
This example is actually from http://www.tutorials-android.com/learn/How_to_encrypt_and_decrypt_strings.rhtml.
SRC Directory Master file:
[Java]View Plaincopy
- 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.getinstance ("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 Skeyspec = 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 Skeyspec = 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" + encryptingcode);
- 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, find "encrypted results for" can see:
Run results (as seen from the log log):
i/encryption result is (): Bfb77d8f1e1ee9d5e252926a12659de8
i/decryption Results (190): 0123456789