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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
加密解密,前端加密,后端解密 (169个子文件)
AESEncryptUtil.class 5KB
DESEncryptUtil.class 4KB
MD5Util.class 2KB
CryptoHelper.class 2KB
Base64EncodeUtil.class 1KB
CodeServlet.class 1KB
.classpath 1KB
org.eclipse.wst.common.component 853B
org.eclipse.wst.jsdt.ui.superType.container 49B
AESEncryptUtil.java 5KB
DESEncryptUtil.java 4KB
MD5Util.java 2KB
CryptoHelper.java 2KB
CodeServlet.java 1KB
Base64EncodeUtil.java 1KB
cipher-core.js 28KB
cipher-core.js 28KB
tripledes.js 23KB
tripledes.js 23KB
core.js 21KB
core.js 21KB
sha512.js 13KB
sha512.js 13KB
sha3.js 10KB
sha3.js 10KB
tripledes-min.js 10KB
tripledes-min.js 10KB
md5.js 9KB
md5.js 9KB
ripemd160.js 9KB
ripemd160.js 9KB
x64-core.js 8KB
x64-core.js 8KB
aes.js 8KB
aes.js 8KB
rabbit-legacy.js 6KB
rabbit-legacy.js 6KB
rabbit.js 6KB
rabbit.js 6KB
sha256.js 5KB
sha256.js 5KB
sha512-min.js 5KB
sha512-min.js 5KB
pbkdf2.js 4KB
pbkdf2.js 4KB
cipher-core-min.js 4KB
cipher-core-min.js 4KB
sha1.js 4KB
sha1.js 4KB
ripemd160-min.js 4KB
ripemd160-min.js 4KB
hmac.js 4KB
hmac.js 4KB
enc-utf16.js 4KB
enc-utf16.js 4KB
evpkdf.js 4KB
evpkdf.js 4KB
enc-base64.js 3KB
enc-base64.js 3KB
core-min.js 3KB
core-min.js 3KB
rc4.js 3KB
rc4.js 3KB
md5-min.js 3KB
md5-min.js 3KB
rabbit-min.js 2KB
rabbit-min.js 2KB
sha3-min.js 2KB
sha3-min.js 2KB
rabbit-legacy-min.js 2KB
rabbit-legacy-min.js 2KB
aes-min.js 2KB
aes-min.js 2KB
mode-ctr-gladman.js 2KB
mode-ctr-gladman.js 2KB
sha384.js 2KB
sha384.js 2KB
lib-typedarrays.js 2KB
lib-typedarrays.js 2KB
mode-cfb.js 2KB
mode-cfb.js 2KB
sha224.js 2KB
sha224.js 2KB
format-hex.js 2KB
format-hex.js 2KB
sha256-min.js 1KB
sha256-min.js 1KB
mode-ctr.js 1KB
mode-ctr.js 1KB
sha1-min.js 1KB
sha1-min.js 1KB
mode-ofb.js 1KB
mode-ofb.js 1KB
pad-ansix923.js 994B
pad-ansix923.js 994B
rc4-min.js 887B
rc4-min.js 887B
enc-utf16-min.js 880B
enc-utf16-min.js 880B
enc-base64-min.js 869B
共 169 条
- 1
- 2
资源评论
- a74592018-07-04还没看 重新下载下
凯歌的博客
- 粉丝: 56
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功