Android AES Encryption algorithm and its implementation

Source: Internet
Author: User

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
  1. Package com.qq;
  2. Import Java.security.SecureRandom;
  3. Import Javax.crypto.Cipher;
  4. Import Javax.crypto.KeyGenerator;
  5. Import Javax.crypto.SecretKey;
  6. Import Javax.crypto.spec.SecretKeySpec;
  7. Import android.app.Activity;
  8. Import Android.os.Bundle;
  9. Import Android.util.Log;
  10. Public class Simplecrypto extends Activity {
  11. public static string encrypt (string seed, string cleartext) throws Exception {
  12. byte[] Rawkey = Getrawkey (Seed.getbytes ());
  13. byte[] result = Encrypt (Rawkey, cleartext.getbytes ());
  14. return Tohex (result);
  15. }
  16. public static string decrypt (string seed, string encrypted) throws Exception {
  17. byte[] Rawkey = Getrawkey (Seed.getbytes ());
  18. byte[] enc = tobyte (encrypted);
  19. byte[] result = Decrypt (rawkey, ENC);
  20. return new String (result);
  21. }
  22. private static byte[] Getrawkey (byte[] seed) throws Exception {
  23. Keygenerator KGen = keygenerator.getinstance ("AES");
  24. securerandom sr = securerandom.getinstance ("sha1prng");
  25. Sr.setseed (seed);
  26. Kgen.init (+, SR); //192 and available
  27. Secretkey skey = Kgen.generatekey ();
  28. byte[] raw = skey.getencoded ();
  29. return raw;
  30. }
  31. private static byte[] Encrypt (byte[] Raw, byte[] Clear) throws Exception {
  32. Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
  33. Cipher Cipher = cipher.getinstance ("AES");
  34. Cipher.init (Cipher.encrypt_mode, Skeyspec);
  35. byte[] Encrypted = cipher.dofinal (clear);
  36. return encrypted;
  37. }
  38. private static byte[] Decrypt (byte[] Raw, byte[] encrypted) throws Exception {
  39. Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES");
  40. Cipher Cipher = cipher.getinstance ("AES");
  41. Cipher.init (Cipher.decrypt_mode, Skeyspec);
  42. byte[] decrypted = cipher.dofinal (encrypted);
  43. return decrypted;
  44. }
  45. public static string Tohex (String txt) {
  46. return Tohex (Txt.getbytes ());
  47. }
  48. public static string Fromhex (String hex) {
  49. return New String (ToByte (hex));
  50. }
  51. public static byte[] ToByte (String hexstring) {
  52. int len = hexstring.length ()/2;
  53. byte[] result = new Byte[len];
  54. For (int i = 0; i < len; i++)
  55. Result[i] = integer.valueof (hexstring.substring (2*i, 2*i+2), + ). Bytevalue ();
  56. return result;
  57. }
  58. public static String Tohex (byte[] buf) {
  59. if (buf = = null)
  60. return "";
  61. StringBuffer result = new StringBuffer (2*buf.length);
  62. For (int i = 0; i < buf.length; i++) {
  63. Appendhex (result, buf[i]);
  64. }
  65. return result.tostring ();
  66. }
  67. Private final static String HEX = "0123456789ABCDEF";
  68. private static void Appendhex (StringBuffer sb, byte b) {
  69. Sb.append (Hex.charat ((b>>4) &0x0f)). Append (Hex.charat (b& 0x0f));
  70. }
  71. /** Called when the activity is first created. * /
  72. @Override
  73. public void OnCreate (Bundle savedinstancestate) {
  74. super.oncreate (savedinstancestate);
  75. Setcontentview (R.layout.main);
  76. String Masterpassword = "a";
  77. String originaltext = "0123456789";
  78. byte[] Text = new byte[]{' 0 ', '1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',  ' 9 '};
  79. byte[] Password = new byte[]{' a '};
  80. try {
  81. String Encryptingcode = Simplecrypto.encrypt (Masterpassword,originaltext);
  82. SYSTEM.OUT.PRINTLN ("Encrypted result is" + encryptingcode);
  83. LOG.I ("Encrypted result is", encryptingcode);
  84. String Decryptingcode = Simplecrypto.decrypt (Masterpassword, Encryptingcode);
  85. System.out.println ("decryption result is" + decryptingcode);
  86. LOG.I ("decryption result", decryptingcode);
  87. } catch (Exception e) {
  88. //TODO auto-generated catch block
  89. E.printstacktrace ();
  90. }
  91. }
  92. }


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

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.