Android AES Encryption tools class sharing

  • 2020-06-07 05:17:56
  • OfStack

1. AES encryption tool class

java does not support PKCS7Padding, only PKCS5Padding. We know that the encryption algorithm is composed of algorithm + pattern + filling. The next article introduces the common AES encryption of iOS and Android. This article USES PKCS5Padding encryption.


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 / model / fill  **/
 private static final String CipherMode = "AES/ECB/PKCS5Padding";
 
 ///**  Create a 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() < 32) {
  sb.append("0");
 }
 if (sb.length() > 32) {
  sb.setLength(32);
 }
 
 try {
  data = sb.toString().getBytes("UTF-8");
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 }
 return new SecretKeySpec(data, "AES");
 }
 
 // /**  Encrypted byte data  **/
 public static byte[] encrypt(byte[] content, String password) {
 try {
  SecretKeySpec key = createKey(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 ( The results for 16 Base 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 = createKey(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;
 }
 
 ///**  decryption 16 A hexadecimal string is 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;
 }
 
 // /**  Byte array converted to 16 Base string  **/
 public static String byte2hex(byte[] b) { // 1 The number of bytes, 
 StringBuffer sb = new StringBuffer(b.length * 2);
 String tmp = "";
 for (int n = 0; n < b.length; n++) {
  //  The integer converted to 106 Hexadecimal said 
  tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  if (tmp.length() == 1) {
  sb.append("0");
  }
  sb.append(tmp);
 }
 return sb.toString().toUpperCase(); //  To uppercase 
 }
 
 // /**  will hex Converts a string to an array of bytes  **/
 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, 16) & 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(savedInstanceState);
 		setContentView(R.layout.activity_main);
	
	 String masterPassword = "a"; 
	 String originalText = " in "; 

	 try { 
	  String encryptingCode = AESHelper.encrypt(originalText,masterPassword); 
//	  System.out.println(" The encryption result is  " + encryptingCode); 
	  Log.i(" The encryption 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 bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

3. Print the result


09-19 10:41:05.467: I/ The encryption result is (707): E55C24701F6380478E1940ADDFD08D22
09-19 10:41:05.467: I/ Decryption result (707):  in 

Related articles: