AES全称 Advanced Encryption Standard, 高级加密算法,更加安全,可取代DES。

Aes:

package com.blog.d201706.encrypt;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key; public class Aes { /**
* 加解密key
*/
private final Key keySpec; /**
* 构造函数
* @param key
*/
public Aes(String key){
keySpec = new SecretKeySpec(key.getBytes(), "AES");
} /**
* 加密
* @param str
* @return
*/
public String encryt(String str) {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
return parseByte2HexStr(cipherByte);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 解密
* @param deCodeStr
* @return
*/
public String decrypt(String deCodeStr) {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
try {
if (null == deCodeStr) return null;
byte[] buff = parseHexStr2Byte(deCodeStr);
Cipher c;
c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, keySpec);
byte[] cipherByte = c.doFinal(buff);
return new String(cipherByte);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} /**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}

MainAes:

package com.blog.d201706.encrypt;

public class MainAes {
public static void main(String[] args) throws Exception {
Aes aes = new Aes("1234567890123456");
String msg = "测试文本:今天周5" + System.currentTimeMillis();
String encontent = aes.encryt(msg);
String decontent = aes.decrypt(encontent);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + encontent);
System.out.println("解密后:" + decontent);
} }

最新文章

  1. QQ邮箱发送邮件,出现mail from address must be same as authorization user错误
  2. sdk manager更新超时 time out
  3. hibernate 配置文件
  4. [Swift]LeetCode134. 加油站 | Gas Station
  5. centos6.7 配置外网端口映射
  6. python note 16 re模块的使用
  7. MATLAB一元线性回归分析
  8. HTML 5 Web 音频
  9. 419. Battleships in a Board 棋盘上的战舰数量
  10. 使用python爬虫爬取股票数据
  11. ChinaCock界面控件介绍-TCCImageViewerForm
  12. Jmeter使用流程及简单分析监控
  13. tensorflow进阶篇-4(损失函数2)
  14. ICC2 常用命令
  15. os模块学习+open行数
  16. POJ 2823 Sliding Window(单调队列 || 线段树)题解
  17. 使用kubernetes的deployment进行RollingUpdate
  18. swift中UITextView的使用
  19. Linux fdisk命令详解[主分区/逻辑分区创建]
  20. 关于Cooperation.GTST

热门文章

  1. git远程删除分支后,本地执行git branch -a依然能看到删除分支到底该咋整?
  2. Session技术入门代码案例
  3. 日志框架之Logger
  4. [Err] ORA-00942: table or view does not exist
  5. 阶段3 3.SpringMVC&#183;_02.参数绑定及自定义类型转换_7 获取Servlet原生的API
  6. springboot中静态属性/静态方法从YAML(yml)读取配置属性
  7. 如何从项目中移除CocoaPods
  8. 让nginx 的ssi支持include相对路径
  9. jQuery I
  10. PJzhang:从js文件中寻找子域名的SubDomainizer