欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android AES加密工具类分享

程序员文章站 2022-04-30 22:58:21
1、aes加密工具类 java不支持pkcs7padding,只支持pkcs5padding。我们知道加密算法由算法+模式+填充组成,下一篇介绍ios和android通用...

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): 于