Android AES Encryption Algorithm and Its Implementation

Source: Internet
Author: User

Yesterday, the boss told me to look at Android encryption.Algorithm. So I found it online and found the AES encryption algorithm. (Of course, there are MD5 and base64 http://snowolf.iteye.com/blog/410860.)ArticleMany of them are listed, but they are basically j2se platforms and not necessarily supported by the Android platform. However, the AES algorithm Android comes with a package, which can be viewed from the official http://developer.android.com/reference/javax/crypto/cipher.html.
)

What is the AES encryption algorithm? You can go to Google, expert-levelProgramThe engineer can use the package.

This example is actually from http://www.tutorials-android.com/learn/how_to_encrypt_and_decrypt_strings.rhtml.

Main file in the src directory:

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 (128, Sr); // 192 and 256 bits may not be 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. 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. 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), 16 ). 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 = "0123456789 abcdef"; 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:" + encryptingcode); log. I ("the encryption result is", encryptingcode); string decryptingcode = simplecrypto. decrypt (masterpassword, encryptingcode); system. out. println ("decryption Result:" + decryptingcode); log. I ("decryption result", decryptingcode);} catch (exception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}


By default, the layout file main. xml and configuration file androidmanifest. XML are ready. The final result is shown in the log. Use ADB logcat> D: \ 1.txtto locate the 1.txt file on the D Drive, open it in notepad, and search for "encrypted result" to see it:

Running result (as shown in the log ):

I/the encrypted result is (190): bfb77d8f1e1ee9d5e252926a12659de8

I/Decryption result (190): 0123456789

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.