在网上找了大半天资料,终于找到一个可以用的

public static class AES {
// 算法名称
final static String KEY_ALGORITHM = "AES";
// 加解密算法/模式/填充方式
final static String algorithmStr = "AES/CBC/PKCS7Padding";
//
private static Key key;
private static Cipher cipher;
boolean isInited = false;

public static class AES {
// 算法名称
final static String KEY_ALGORITHM = "AES";
// 加解密算法/模式/填充方式
final static String algorithmStr = "AES/CBC/PKCS7Padding";
//
private static Key key;
private static Cipher cipher;
boolean isInited = false;

public static void init(byte[] keyBytes) {

// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
// 转化成JAVA的密钥格式
key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
try {
// 初始化cipher
cipher = Cipher.getInstance(algorithmStr, "BC");
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 加密方法
*
* @param content 要加密的字符串
* @param keyBytes 加密密钥
* @return
*/
@SuppressWarnings("restriction")
public static String encrypt(byte[] content, byte[] keyBytes, byte[] iv) {
byte[] encryptedText = null;
init(keyBytes);
try {
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
encryptedText = cipher.doFinal(content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

BASE64Encoder encoder = new sun.misc.BASE64Encoder();
return encoder.encode(encryptedText);
}

/**
* 解密方法
*
* @param encryptedData 要解密的字符串
* @param keyBytes 解密密钥
* @return
*/
public byte[] decrypt(byte[] encryptedData, byte[] keyBytes) {
/*
* byte[] encryptedText = null; init(keyBytes); System.out.println("IV:" + new
* String(iv)); try { cipher.init(Cipher.DECRYPT_MODE, key, new
* IvParameterSpec(iv)); encryptedText = cipher.doFinal(encryptedData); } catch
* (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
* return encryptedText; }
*/
return null;
}
}

最新文章

  1. [转]nopCommerce Widgets and How to Create One
  2. 对比Haproxy和Nginx负载均衡效果
  3. C和指针 第九章 习题
  4. OC的类别(分类)和拓展
  5. Python之路----------ConfigParser模块
  6. ASP数组全集,多维数组和一维数组[转]
  7. 【开源项目10】安卓图表引擎AChartEngine
  8. android 打包签名
  9. [MODx] 4. getResources
  10. c#实现microsoft账号登入授权(OAuth 2.0)并获取个人信息
  11. windows service编程
  12. oracle 对表的操作
  13. 4月23日 db 命令操作 和表操作
  14. bzoj4481非诚勿扰(期望dp)
  15. 微信小程序--WXS---JS 代码插入
  16. Android support 26.0.0-alpha1 产生的问题(zz)
  17. .NET Core容器化开发系列(一)——Docker里面跑个.NET Core
  18. QtPropertyBrowser+vs2010的安装与配置
  19. Nginx 系统维护配置
  20. Aop检查Session,全局过滤器和No全局过滤器

热门文章

  1. 20164318 毛瀚逸 Exp1 PC平台逆向破解
  2. SQL server 多个字段设为主键
  3. Linux上使用源代码安装软件
  4. Yii2.0 RESTful API 基础配置教程
  5. HISI VENC 实际输出帧率控制
  6. 2018-2019-2 20165308网络对抗技术 Exp6:信息收集与漏洞扫描
  7. [ZZ] 边缘检测 梯度与Roberts、Prewitt、Sobel、Lapacian算子
  8. Tomcat 多个虚拟主机配置方法
  9. 【总结】Java异常分类
  10. SSM框架-MyBatis框架数据库的增删查改操作