Android AES Encryption Tool class sharing _android

Source: Internet
Author: User
Tags decrypt stringbuffer

1, AES Encryption tool class

Java does not support pkcs7padding and supports only pkcs5padding. We know that the encryption algorithm by the algorithm + mode + fill composition, the next introduction of iOS and Android general AES encryption, this article uses pkcs5padding encryption method.

Package Com.example.aesdemo;
Import java.io.UnsupportedEncodingException;
Import Javax.crypto.Cipher;
 
Import Javax.crypto.spec.SecretKeySpec; * * AES symmetric encryption decryption class **/public class Aeshelper {///** algorithm/mode/fill **/private static final String Ciphermode = "aes/ecb/pk
 
 Cs5padding ";
 * * 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 () <) {Sb.append ("0");
 } if (Sb.length () >) {sb.setlength (32);
 try {data = sb.tostring (). GetBytes ("UTF-8");
 catch (Unsupportedencodingexception e) {e.printstacktrace ();
 Return to new Secretkeyspec (data, "AES"); }///** encrypted byte data **/public static byte[] Encrypt (byte[] content, String password) {try {secretkeyspec key = creat
  EKey (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;
 ///** encryption (Result 16 binary 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 byte array **/public static byte[] Decrypt (byte[] content, String password) {try {secretkeyspec key = creat
  EKey (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;
 ///** decrypts the 16-binary 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; The///** byte array is converted to a 16 feed 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++) {//integer to hexadecimal indicates tmp = (java.lang.Integer.toHexString (b[n) & 0XFF));
  if (tmp.length () = = 1) {sb.append ("0");
 } sb.append (TMP); Return sb.tostring (). toUpperCase ();  Turn uppercase}///** hex string to 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,) & 0xFF);
 return result; }
}

2. Use

New Android Project

Package Com.example.aesdemo;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu; 

Import Android.util.Log; public class Mainactivity extends activity {protected void OnCreate (Bundle savedinstancestate) {super.oncreate (SA
 		Vedinstancestate);
	
	 Setcontentview (R.layout.activity_main); 
	 String Masterpassword = "a"; 

	 String originaltext = "to"; 
try {String Encryptingcode = Aeshelper.encrypt (Originaltext,masterpassword); 
	  SYSTEM.OUT.PRINTLN ("Encrypted result is" + encryptingcode); 
	  LOG.I ("Encrypted result is", encryptingcode); 
String Decryptingcode = Aeshelper.decrypt (Encryptingcode,masterpassword); 
	  System.out.println ("decryption result is" + decryptingcode); 
	  LOG.I ("decryption result", decryptingcode); 
	 catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action B
		Ar if it is present. Getmenuinflater (). Inflate (r.menu.mAin, menu);
	return true; }
}

3, printing results

09-19 10:41:05.467:i/Encryption Result (707): E55c24701f6380478e1940addfd08d22
09-19 10:41:05.467:i/decryption Result (707):
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.