版权声明:博客对我来说是记忆的笔记和知识的分享~非常感谢博客大神的帮助,若有无意侵权,请您联系我,谢谢^_^!转载请声明出处: https://blog.csdn.net/qq_36698956/article/details/85285756
前言
因为现在做的项目数据需要进行加密,使其在数据库中看到的是密文,所以先通过网上的资料写了一个加密工具类。
加密代码
package com.iwork.platform.util;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* 加密解密工具
*
* @author ykc
*
*/
public class EncryptAndDecrypt {
// 加密长度
private final static int LENGTH = 128;
// 编码方式
private final static String ENCODE = "UTF-8";
// 秘钥
private final static String defaultKey = "sdit606.";
// 前缀
private final static String defaultPrefix = "SDITENCRYPT";
public static void main(String[] args) {
String mes = "测试数据";
String e = encrypt(mes);
System.out.println("加密后:" + e);
System.out.println("解密后:" + decrypt(e));
}
/**
* AES加密后再使用BASE64加密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
* @param content
* @return
* @throws Exception
*/
public static String encrypt(String content) {
String value = "";
try {
if(!isEmpty(content)){
value = defaultPrefix + base64Encode(aesEncryptToBytes(content));
}
} catch (Exception e) {
System.out.println("EncryptAndDecrypt(加密错误)");
e.printStackTrace();
}
return value;
}
/**
* AES解密后再使用BASE64解密
* 增加前缀
* 1.辨识正常数据,使其不进行解密
* 2.提高安全度
* @param encryptStr
* @return
* @throws Exception
*/
public static String decrypt(String encryptStr) {
String value = "";
try {
int length = defaultPrefix.length();
if (encryptStr.length() > length) {
String val = encryptStr.substring(0, length);
if (val.equals(defaultPrefix)) {
value = aesDecryptByBytes(base64Decode(encryptStr.substring(length)));
} else {
value = encryptStr;
}
} else {
value = encryptStr;
}
} catch (Exception e) {
System.out.println("EncryptAndDecrypt(解密错误)");
e.printStackTrace();
}
return value;
}
/**
* AES加密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] aesEncryptToBytes(String content) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// 兼容Win和Linux核心代码 --开始
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(defaultKey.getBytes());
kgen.init(LENGTH, secureRandom);
// 兼容Win和Linux核心代码 --结束
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
return cipher.doFinal(content.getBytes(ENCODE));
}
/**
* AES解密
*
* @param encryptBytes
* @return
* @throws Exception
*/
public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
// 兼容Win和Linux核心代码 --开始
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(defaultKey.getBytes());
kgen.init(LENGTH, secureRandom);
// 兼容Win和Linux核心代码 --结束
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec sks = new SecretKeySpec(kgen.generateKey().getEncoded(), "AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
/**
* BASE64 加密
*
* @param content
* @return
* @throws Exception
*/
public static String base64Encode(byte[] bytes) {
return new BASE64Encoder().encode(bytes);
}
/**
* BASE64 解密
*
* @param content
* @return
* @throws Exception
*/
public static byte[] base64Decode(String base64Code) throws Exception {
return isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
public static boolean isEmpty(String str) {
return null == str || "".equals(str.trim());
}
}
加密结果
加密后:SDITENCRYPTTMidf05lpkkSrHpX2nVKHA==
解密后:测试数据
加前缀是因为项目中已存在大量数据,加上前缀既能判断是否为原始数据,还能增加一点安全性(虽然并没什么卵用。)
Linux环境要求
环境要求:JDK1.7
Linux环境中,必须是Oracle JDK, 千万不要使用Open jdk, 否者加密的算法结果跟win系统不一致(在测试中使用OpenJDK1.8居然可以,1.7不行),具体的jdk问题请参考此篇文章http://blog.51cto.com/leo01/1795447
PS
如果你使用CAS sso单点登录,想自定义加密的话(不使用Md5 ,可以解密起来不太方便),请查看博主这篇文章https://blog.csdn.net/qq_36698956/article/details/81781622 CAS单点登录自定义加密O(∩_∩)O~
以上操作博主亲自操刀过,无出任何问题,有异常的小伙伴可以留言给我,我会回复你的哦 σ`∀´)σ