java AES 加密解密工具(Advanced Encryption Standard)发现明文相同但每次重启服务后密文就会不同于是有了改进
1、通用方法
package com.qlkj.hzd.commom.utils; import javax.crypto.*;
import java.io.UnsupportedEncodingException;
import java.security.*; /**
* RSA相关工具类
*
* @author vampire
* @date 2018/10/12 下午5:33
*/
public class EncrypAES { //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
private static KeyGenerator keygen;
//SecretKey 负责保存对称密钥
private static SecretKey deskey;
//Cipher负责完成加密或解密工作
private static Cipher c;
//该字节数组负责保存加密的结果
private static byte[] cipherByte; static {
try {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
//实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
keygen = KeyGenerator.getInstance("AES");
//生成密钥
deskey = keygen.generateKey();
//生成Cipher对象,指定其支持的DES算法
c = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} } /**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String Encrytor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes("utf-8");
// 加密,结果保存进cipherByte
cipherByte = c.doFinal(src);
return parseByte2HexStr(cipherByte);
} /**
* 对字符串解密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String Decryptor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(parseHexStr2Byte(str));
return new String(cipherByte);
} /**
* 将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;
} /**
* 将二进制转换成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();
} }
2、添加密码加解密
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(content);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
3、由于每次启动服务生产的
SecretKey secretKey = kgen.generateKey(); 会变 所以在 下边初始化时就将key写死。
package com.qlkj.hzd.commom.utils; import com.alibaba.fastjson.JSONObject; import javax.crypto.*;
import java.io.UnsupportedEncodingException;
import java.security.*; /**
* RSA相关工具类
*
* @author vampire
* @date 2018/10/12 下午5:33
*/
public class EncrypAES { //KeyGenerator 提供对称密钥生成器的功能,支持各种算法
private static KeyGenerator keygen;
//SecretKey 负责保存对称密钥
private static SecretKey deskey = new SecretKey() {
@Override
public String getAlgorithm() {
return "AES";
} @Override
public String getFormat() {
return "RAW";
} @Override
public byte[] getEncoded() {
MessageDigest md = null;
byte[] thedigest = null;
try {
md = MessageDigest.getInstance("MD5");
thedigest = md.digest("pe1mUKtDZWxX2O41ea5+Tg==".getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return thedigest;
}
};
//Cipher负责完成加密或解密工作
private static Cipher c;
//该字节数组负责保存加密的结果
private static byte[] cipherByte; static {
try {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
//实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
keygen = KeyGenerator.getInstance("AES");
//生成密钥
// deskey = keygen.generateKey();
System.out.println(JSONObject.toJSONString(deskey));
//生成Cipher对象,指定其支持的DES算法
c = Cipher.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} } /**
* 对字符串加密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String encrytor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] src = str.getBytes("utf-8");
// 加密,结果保存进cipherByte
cipherByte = c.doFinal(src);
return parseByte2HexStr(cipherByte);
} /**
* 对字符串解密
*
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String decryptor(String str) throws InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey);
cipherByte = c.doFinal(parseHexStr2Byte(str));
return new String(cipherByte);
} /**
* 将16进制转换为二进制
* * @param hexStr
* * @return
*/
private 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;
} /**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
private 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();
} }
java AES 加密解密工具(Advanced Encryption Standard)发现明文相同但每次重启服务后密文就会不同于是有了改进的更多相关文章
- Java AES加密解密工具 -- GUI 、在线传输文件
原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下: (1)将128位AES ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- C# 实现 JAVA AES加密解密[原创]
以下是网上普遍能收到的JAVA AES加密解密方法. 因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类.无法使用安全随机数生成KEY. 我们在接收J ...
- AES加密解密工具类封装(AESUtil)
package club.codeapes.common.utils; import org.springframework.util.Base64Utils; import javax.crypto ...
- C#与Java AES 加密解密
参考文档:https://www.cnblogs.com/xbzhu/p/7064642.html 前几天对接Java接口,需要C#加密参数,Java解密.奈何网上找了一堆大同小异的加解密方法都跟Ja ...
- JAVA AES加密解密
import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java ...
- 自写AES加密解密工具类
此类主要用于加密与解密,采用128位ECB模式,PKCS5Padding填充补位. 可使用方法为加密返回二进制encryptBin(content, key).加密返回十六进制encryptHex(c ...
- java中加密解密工具类
在工作中经常遇到需要加密.解密的场景.例如用户的手机号等信息,在保存到数据库的过程中,需要对数据进行加密.取出时进行解密. public class DEStool { private String ...
- Java使用AES加密解密
AES加密机制: 密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准. 这个标准用来替代原先的 ...
随机推荐
- VMware 15.0下载及安装教程
虚拟机 VMware WorkStation Pro15 下载及安装详细解 9虚拟机 VMware WorkStation Pro15 下载及安装详细解. 虚拟机官方网站: https://www.v ...
- Python里//与/的区别?
1.Python里面//的作用是除法取整,也就是直接取整数部分 例如:5//6=0; 56//3=18 2.而/的作用是直接进行常规的除法运算 例如:56/8=7 程序运算实例如下:
- Java开发工程师(Web方向) - 02.Servlet技术 - 第2章.Cookie与Session
第2章--Cookie与Session Cookie与Session 浏览器输入地址--HTTP请求--Servlet--HTTP响应--浏览器接收 会话(session):打开浏览器,打开一系列页面 ...
- python学习笔记04 --------------基本运算符
1.算数运算 + 加 - 减 * 乘 / 除 % 取模(先做除法,然后返回余数) ** 乘方(幂运算) // 取整(相除,然后返回商的整数部分) 2.比较运算(返回布尔值) == ...
- python程序设计——面向对象程序设计:类
理解面向对象 基本原则是,计算机程序由多个能够起到子程序作用的单元或对象组合而成 关键性观念是,数据以及对数据的操作封装在一起,组成一个相互依存.不可分割的整体,即对象 python面向对象特性 完全 ...
- 日历(Calendar)模块
#usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...
- Python3 Tkinter-Canvas
1.创建 from tkinter import * root=Tk() cv=Canvas(root,bg='black') cv.pack() root.mainloop() 2.创建item f ...
- POJ 1741 Tree(树的分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- nginx虚拟目录实现两个后台使用
购买了阿里云机器,准备搭建一套备份的后台,由于资源有限所以将两个后台搭建到一组SLB下的两台WEB上. 使用软件:NGINX+PHP root@xx conf.d]# yum install php- ...
- 11.22Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.979 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.978 实现视频浏览的功能 王 ...