iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android AES加密工具类分享
  • 801
分享到

Android AES加密工具类分享

工具类aes加密工具aesAndroid 2022-06-06 10:06:54 801人浏览 薄情痞子
摘要

1、AES加密工具类 java不支持PKCS7Padding,只支持PKCS5Padding。我们知道加密算法由算法+模式+填充组成,下一篇介绍iOS和Android通用的AE

1、AES加密工具类

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


package com.example.aesdemo;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecreTKEySpec;
//
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;
 }
 //
 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;
 }
 //
 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;
 }
 // 
 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(); // 转成大写
 }
 // 
 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封装的Http请求实用工具类Android实用的Toast工具类封装Android工具栏顶出转场动画的实现方法实例Android 数据存储之 FileInputStream 工具类及FileInputStream类的使用android自动工具类TextUtils使用详解android实用工具类分享(获取内存/检查网络/屏幕高度/手机分辨率)android开发教程之实现toast工具类Android开发中日期工具类DateUtil完整实例Android动画工具类的封装实战记录


--结束END--

本文标题: Android AES加密工具类分享

本文链接: https://www.lsjlt.com/news/26908.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作