package com.zzy.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.junit.Test;
public class AESEncryptUtil {
public static void main(String[] args){
String content = "test12321郭凯歌";
String password = "12345678";
System.out.println("加密前:" + content);
// 加密
String encryptResult = encrypt(content, password);
System.out.println("加密后:" + encryptResult);
// 解密
String decryptResult = decrypt(encryptResult, password);
System.out.println("解密后:" + decryptResult); //解密编码设置为utf-8就可以了
}
@Test
public void testAes(){
// 解密
String decryptResult = decrypt("NWMwMWY4Y2M2ZWJiNjFiODNmMjI1NmQzYTdmMzlhNmE=", "Secret Passphrase");
System.out.println("解密后:" + decryptResult); //解密编码设置为utf-8就可以了
}
/**
* Description 根据键值进行加密
* @param data
* @param key 加密键byte数组
* @return
* @throws Exception
*/
public static String encrypt(String data, String key){
byte[] bt = encrypt(data, key,"utf-8");
String strs = Base64.getEncoder().encodeToString(bt);
return strs;
}
/**
* Description 根据键值进行解密
* @param data
* @param key 加密键byte数组
* @return
* @throws UnsupportedEncodingException
* @throws IOException
* @throws Exception
*/
public static String decrypt(String data, String key){
if (data == null)
return null;
byte[] buf = Base64.getDecoder().decode(data);
System.out.println(new String(buf));
byte[] bt = decrypt(buf,key);
return new String(bt);
}
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
*/
public static byte[] encrypt(String content, String password ,String charset) {
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");// 创建密码器
byte[] byteContent = content.getBytes(charset);
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return result; // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param content
* 待解密内容
* @param password
* 解密密钥
* @return
*/
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;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2ToByte16Str(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[] parseByte16StrToByte2(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;
}
}
凯歌的博客
- 粉丝: 82
- 资源: 8
最新资源
- [上传下载]ExtJS 2.2 开源网络硬盘系统_dogdisk.rar
- [上传下载]仿163网盘无刷新文件上传 for Jsp_fileupload_jsp.rar
- springboot-付费自习室管理系统.zip
- [上传下载]宏软JSP上传系统 v2.0完美版_upload-v2.rar
- [上传下载]宏软JSP上传系统 v1.0_hrjspup10.rar
- [上传下载]手机电影下载_mobiledy.rar
- [上传下载]南京同庆水若寒开发的下载系统_downjsp.rar
- [上传下载]铁人下载系统 Liuxing 1.0_liuxing1.0.rar
- [搜索链接]深度学习网址导航系统 v0.0.16(jsp)_jspurl.rar
- [搜索链接]淘特搜索引擎共享版_tot_search_engine.rar
- [搜索链接]Mysoo站内搜索 v1.0 Peview_mysoo-1.0-preview.war.rar
- [搜索链接]相见欢友情链接系统ASPX版 v1.0_xjlinkaspxv1.0.rar
- [图片动画]JSP目录直读程序_jsp_mlzd.rar
- [搜索链接]要广告分类系统 v2.0_yad20.rar
- [新闻文章]bay_news V1.0.0 北雨新闻信息管理系统_bynews.rar
- [新闻文章]eNews新闻发布系统v1.0_enewsv1.0mysql.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈