需求背景
MD5/RSA/SHA1/AES/DES
,自定义一个可以加解密的工具类;话不多说,上代码
/**
* 字节数组加密
*
* @param bytes 入参
* @return 出参
*/
public byte[] internalEncrypt(byte[] bytes) {
//获取16以内的随机数
Random random = new Random();
int seed = random.nextInt(xorKey.length);
//数组长度
int len = bytes.length;
//新数组
byte[] newBytes = new byte[len + 5];
//随机数放到新数组首位
newBytes[0] = (byte) seed;
//数据长度放到新数据前面
convertByte(newBytes, len);
//新数组初始下标
int m = 5;
//需要加密数据跟秘钥进行异或操作
for (int i = 0; i < len; i++) {
newBytes[i + m] = (byte) (bytes[i] ^ xorKey[seed % xorKey.length]);
seed++;
}
return newBytes;
}
/**
* 字节数组解密
*
* @param bytes 入参
* @return 出参
*/
public byte[] internalDecrypt(byte[] bytes) {
if (bytes.length < 6) {
return new byte[0];
}
//获取加密时生成的随机数
int seed = bytes[0];
//校验随机数
if (seed > xorKey.length - 1) {
return bytes;
}
//校验是否是加密数据,校验数据长度
int len = convertInt(bytes);
if (bytes.length != len + 5) {
return bytes;
}
//反向异或解密
byte[] newBytes = new byte[len];
for (int i = 5; i < bytes.length; i++) {
newBytes[i - 5] = (byte) (bytes[i] ^ xorKey[seed % xorKey.length]);
seed++;
}
return newBytes;
}
/**
* 字符串加密
*
* @param str 入参
* @return 出参
*/
public String encrypt(String str) {
if (str == null || str.isEmpty()) {
return "";
}
//字符串转byte
byte[] strByte;
byte[] newByte;
try {
strByte = str.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
newByte = internalEncrypt(strByte);
return replaceBase64(newByte);
}
/**
* 字符串解密
*
* @param str 入参
* @return 出参
*/
public String decrypt(String str) {
if (str == null || str.isEmpty()) {
return "";
}
//特殊字符替换
str = strReplace(str);
//base64解密
Base64.Decoder decoder = Base64.getDecoder();
byte[] bytes = decoder.decode(str);
//字节数组解密
byte[] newBytes = internalDecrypt(bytes);
//获取原始数据
try {
return new String(newBytes, "GBK");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* 静态加密
*
* @param str 入参
* @return 出参
*/
public String staticEncrypt(String str) {
byte[] bytes;
try {
bytes = str.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
//新数组
byte[] newBytes = new byte[bytes.length];
byte sumCode = 0;
//异或操作
for (int i = 0; i < bytes.length; i++) {
int xorNum = bytes[i] ^ staticXorKey[i % staticXorKey.length];
newBytes[i] = (byte) xorNum;
if (i == 0) {
sumCode = newBytes[i];
} else {
sumCode = (byte) (sumCode ^ newBytes[i]);
}
}
//拼接字符
StringBuilder sb = new StringBuilder();
//以16进制存储(大写)
sb.append(Integer.toHexString(sumCode & 0xFF).toUpperCase());
for (byte b : newBytes) {
// 将ASCII值转换为十六进制表示形式
String xorStr = Integer.toHexString(b & 0xFF).toUpperCase();
//不足两位,补全两位
if (xorStr.length() < 2) {
xorStr = "0" + xorStr;
}
sb.append(xorStr);
}
return sb.toString();
}
/**
* 静态解密
*
* @param str 入参
* @return 出参
*/
public String staticDecrypt(String str) {
if (str==null||str.isEmpty()) {
return "";
}
//异或处理的字符串有个位数的解密会失败,直接返回原字符串
if (str.length() % 2 != 0) {
return str;
}
//校验是否为16进制字符
for (char c : str.toCharArray()) {
if (hExChar.indexOf(c) == -1) {
return str;
}
}
byte[] bytes = new byte[str.length() / 2];
for (int i = 0; i < bytes.length; i++) {
int seed = i * 2; // 每两位作为一个字节
// 获取高四位的值
int highNibble = Character.digit(str.charAt(seed), 16);
// 获取低四位的值
int lowNibble = Character.digit(str.charAt(seed + 1), 16);
// 合并高低四位得到完整的byte值
bytes[i] = (byte) ((highNibble << 4) | lowNibble);
}
byte sumCode = 0;
for (int i = 1; i < bytes.length; i++) {
if (i == 1) {
sumCode = bytes[i];
} else {
sumCode = (byte) (sumCode ^ bytes[i]);
}
}
if (sumCode != bytes[0]) {
return str;
}
//如果str长度为4,且前两个字符和后两个字符相等,则不可能是加密后的数据(正常字符的B1异常或后不可能和原文相同)
if (str.length() == 4 && str.substring(0, 2).equals(str.substring(2, 4))) {
return str;
}
byte[] decodeBytes = new byte[bytes.length - 1];
for (int i = 1; i < bytes.length; i++) {
decodeBytes[i - 1] = (byte) (bytes[i] & 0xFF ^ staticXorKey[(i - 1) % staticXorKey.length]);
}
try {
return new String(decodeBytes, "GBK");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
/**
* base64特殊字符替换(正向)
*
* @param bytes 入参
* @return 结果
*/
private String replaceBase64(byte[] bytes) {
Base64.Encoder encoder = Base64.getEncoder();
bytes = encoder.encode(bytes);
String str = new String(bytes, StandardCharsets.UTF_8);
return str.replace("+", "-").replace("/", "_");
}
/**
* base64特殊字符替换(反向)
*
* @param str 入参
* @return 结果
*/
private String strReplace(String str) {
return str.replace("-", "+").replace("_", "/");
}
/**
* 把int按位存入到byte数组中
*
* @param sourceByte byte数组
* @param value int值
* @return 填充后的数组
*/
private byte[] convertByte(byte[] sourceByte, int value) {
//获取第1个byte赋值给数组 ,“>>>”“零扩展”:无论正负,都在高位插入0
sourceByte[1] = (byte) (0);
//获取第2个byte赋值给数组
sourceByte[2] = (byte) (value >> 16 & 0xFF);
//获取第3个byte赋值给数组
sourceByte[3] = (byte) (value >> 8 & 0xFF);
//获取第4个byte赋值给数组
sourceByte[4] = (byte) (value & 0xFF);
return sourceByte;
}
/**
* byte数组转为int
*
* @param bytes 源byte数组
* @return int数据
*/
private int convertInt(byte[] bytes) {
return (bytes[1] & 0xFF) << 24 |
(bytes[2] & 0xFF) << 16 |
(bytes[3] & 0xFF) << 8 |
(bytes[4] & 0xFF);
}
//加密秘钥
private final byte[] xorKey = {0X28, 0X51, 0X22, 0X31, 0X18, 0X16, 0X15, 0X13, 0X18, 0X16, 0X10, 0X14};
//静态秘钥
private final byte[] staticXorKey = {0X21, 0X18, 0X22, 0X21, 0X28, 0X26, 0X15, 0X23, 0X28, 0X26, 0X10, 0X21};
//16进制静态字符
private final String hExChar = "0123456789abcdefABCDEF";
@Test
void test2() {
EncryptUtil encryptUtil = new EncryptUtil();
String result = encryptUtil.encrypt("hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩,hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩");
logger.info("加密结果:" + result);
String newResult = encryptUtil.decrypt(result);
logger.info("解密结果:" + newResult);
}
@Test
void test5() {
EncryptUtil encryptUtil = new EncryptUtil();
String result = encryptUtil.staticEncrypt("hello word 1234 浑浑噩噩 饕餮盛宴 魑魅魍魉 ①hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩,hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ⑩hello word 1234 你好 饕餮盛宴 魑魅魍魉 ");
logger.info("加密结果:" + result);
String newResult = encryptUtil.staticDecrypt(result);
logger.info("解密结果:" + newResult);
}
学习是痛苦的,成长是快乐的!