Android AES加密工具类分享

所属分类: 软件编程 / Android 阅读数: 1914
收藏 0 赞 0 分享

1、AES加密工具类

java不支持PKCS7Padding,只支持PKCS5Padding。我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AES加密,本篇文章使用PKCS5Padding加密方式。

package com.example.aesdemo;

import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
///** AES对称加密解密类 **/
public class AESHelper {
 
 // /** 算法/模式/填充 **/
 private static final String CipherMode = "AES/ECB/PKCS5Padding";
 
 ///** 创建密钥 **/
 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");
 }
 
 // /** 加密字节数据 **/
 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;
 }
 
 ///** 加密(结果为16进制字符串) **/
 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;
 }
 
 // /** 解密字节数组 **/
 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;
 }
 
 ///** 解密16进制的字符串为字符串 **/
 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;
 }
 
 // /** 字节数组转成16进制字符串 **/
 public static String byte2hex(byte[] b) { // 一个字节的数,
 StringBuffer sb = new StringBuffer(b.length * 2);
 String tmp = "";
 for (int n = 0; n < b.length; n++) {
  // 整数转成十六进制表示
  tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
  if (tmp.length() == 1) {
  sb.append("0");
  }
  sb.append(tmp);
 }
 return sb.toString().toUpperCase(); // 转成大写
 }
 
 // /** 将hex字符串转换成字节数组 **/
 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、使用

新建Android工程

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 = "于"; 

	 try { 
	  String encryptingCode = AESHelper.encrypt(originalText,masterPassword); 
//	  System.out.println("加密结果为 " + encryptingCode); 
	  Log.i("加密结果为 ",encryptingCode); 
	  String decryptingCode = AESHelper.decrypt(encryptingCode,masterPassword); 
//	  System.out.println("解密结果为 " + decryptingCode); 
	  Log.i("解密结果",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、打印结果

09-19 10:41:05.467: I/加密结果为(707): E55C24701F6380478E1940ADDFD08D22
09-19 10:41:05.467: I/解密结果(707): 于
更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多