博文介绍:
由于个人工作频繁对接第三系统平台,数据通讯时常用加密保证安全。为了方便后续
使用和对接,用于记录和积累的作用,便于需要的同学自取。
已支持加密:(key:密钥,iv:矢量 eg: key:16指密钥长度为16个字符)
1. Base64
2. MD5(32位)、HmacMD5
3. SHA1、SHA-256
4. DES/CBC/PKCS5Padding (key:8 iv:8)、DES3/CBC/PKCS5Padding(key:24 iv:8)
5. AES/CBC/PKCS5Padding (key:16,iv:16)
注 ** :AES/DES/DES3 的加密数据的字符集 UTF-8 **
public class EncryptUtil {
// Base64 ------------------
public static String base64Enc(String src) {
if (src == null || src.length() == 0 || src.trim().length() == 0) return "";
return new String(base64Enc(src.getBytes()));
}
public static String base64Dec(String src) {
if (src == null || src.length() == 0 || src.trim().length() == 0) return "";
return new String(base64Dec(src.getBytes()));
}
public static byte[] base64Enc(byte[] bytes) {
return Base64.getEncoder().encode(bytes);
}
public static byte[] base64Dec(byte[] bytes) {
return Base64.getDecoder().decode(bytes);
}
// MD5 ----------------------
public static byte[] md5Enc(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(bytes);
return md.digest();
} catch (Exception e) {
}
return null;
}
// source : 加密串 , defLower : 是否默认小写,即ture:小写,false:大写
public static String md5Enc(String source, boolean defLower) {
if (source == null || source.length() == 0 || source.trim().length() == 0) return null;
try {
byte[] bytes = source.getBytes("utf-8");
String hex = new BigInteger(1, md5Enc(bytes)).toString(16);
return defLower ? hex.toLowerCase() : hex.toUpperCase();
} catch (Exception e) {
}
return null;
}
// HMacMD5 ------------------
public static String hmacMD5(String key, String data, boolean defLower) {
if (data == null || data.length() == 0 || data.trim().length() == 0) return null;
try {
byte[] keyByte = key.getBytes("UTF-8");
byte[] dataByte = data.getBytes("UTF-8");
String hex = new BigInteger(1, hmacMD5(keyByte, dataByte)).toString(16);
return defLower ? hex.toLowerCase() : hex.toUpperCase();
} catch (Exception e) {
}
return null;
}
public static byte[] hmacMD5(byte[] key, byte[] data) {
/*
* HmacMd5 calculation formula: H(K XOR opad, H(K XOR ipad, text))
* HmacMd5 计算公式:H(K XOR opad, H(K XOR ipad, text))
* 使用MD5算法 : H代表hash算法,K代表密钥,text代表要加密的数据 ipad为0x36,opad为0x5C。
*/
int length = 64;
byte[] ipad = new byte[length];
byte[] opad = new byte[length];
for (int i = 0; i < 64; i++) {
ipad[i] = 0x36;
opad[i] = 0x5C;
}
// If key's length is longer than 64,then use hash to digest it and use the result as actual key. 。
byte[] actualKey = key.length > length ? md5Enc(key) : key;
// Key bytes of 64 bytes length : [密钥长度64字节]
byte[] keyArr = new byte[length];
for (int i = 0; i < actualKey.length; i++) {
keyArr[i] = actualKey[i];
}
// append zeros to K : [如果密钥长度不足64字节,就使用0x00补齐到64字节。]
if (actualKey.length < length) {
for (int i = actualKey.length; i < keyArr.length; i++)
keyArr[i] = 0x00;
}
// calc K XOR ipad : [使用密钥和ipad进行异或运算]
byte[] kIpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kIpadXorResult[i] = (byte) (keyArr[i] ^ ipad[i]);
}
// append "text" to the end of "K XOR ipad" : [将待加密数据追加到K XOR ipad计算结果后面]
byte[] firstAppendResult = new byte[kIpadXorResult.length + data.length];
for (int i = 0; i < kIpadXorResult.length; i++) {
firstAppendResult[i] = kIpadXorResult[i];
}
for (int i = 0; i < data.length; i++) {
firstAppendResult[i + keyArr.length] = data[i];
}
// calc H(K XOR ipad, text) : [使用哈希算法计算上面结果的摘要]
byte[] firstHashResult = md5Enc(firstAppendResult);
// calc K XOR opad : [使用密钥和opad进行异或运算]
byte[] kOpadXorResult = new byte[length];
for (int i = 0; i < length; i++) {
kOpadXorResult[i] = (byte) (keyArr[i] ^ opad[i]);
}
// append "H(K XOR ipad, text)" to the end of "K XOR opad" : [将H(K XOR ipad, text)结果追加到K XOR opad结果后面]
byte[] secondAppendResult = new byte[kOpadXorResult.length + firstHashResult.length];
for (int i = 0; i < kOpadXorResult.length; i++) {
secondAppendResult[i] = kOpadXorResult[i];
}
for (int i = 0; i < firstHashResult.length; i++) {
secondAppendResult[i + keyArr.length] = firstHashResult[i];
}
// H(K XOR opad, H(K XOR ipad, text)) : [对上面的数据进行哈希运算]
return md5Enc(secondAppendResult);
}
// SHA-1 ----------------------
public static byte[] shaEnc(byte[] data) {
try {
MessageDigest sha = MessageDigest.getInstance("SHA");
sha.update(data);
return sha.digest();
} catch (Exception e) {
}
return null;
}
public static String shaEnc(String data, boolean defLower) {
if (data == null || data.trim().length() == 0) return "";
try {
String hex = new BigInteger(1, shaEnc(data.getBytes())).toString(16);
return defLower ? hex.toLowerCase() : hex.toUpperCase();
} catch (Exception e) {
}
return null;
}
// SHA-256----------------------
public static byte[] sha256Enc(byte[] data) {
try {
MessageDigest sha = MessageDigest.getInstance("SHA-256");
sha.update(data);
return sha.digest();
} catch (Exception e) {
}
return null;
}
public static String sha256Enc(String data, boolean defLower) {
if (data == null || data.trim().length() == 0) return "";
try {
String hex = new BigInteger(1, sha256Enc(data.getBytes())).toString(16);
return defLower ? hex.toLowerCase() : hex.toUpperCase();
} catch (Exception e) {
}
return null;
}
// AES CBC PKCS5Padding 128位----
// data : 加密字符串(UTF-8) , sKey: 密钥key , sIv : 密钥iv (注意:16位长度)
public static String aesEnc(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 16 || data == null) {
return null;
}
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//参数解释:"算法/模式/补码方式"
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");//AES算法:通过密钥,增强加密算法强度
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());//CBC模式:通过向量iv,可增加加密算法的强度"16位"
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));//加密数据
return new String(base64Enc(encrypted)).replaceAll("\r|\n", "");
} catch (Exception e) {
}
return null;
}
// data : 解密字符串(UTF-8) , sKey: 密钥key , sIv : 密钥iv (注意:16位长度)
public static String aesDec(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 16) {
return null;
}
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
byte[] decrypted = base64Dec(data.getBytes("UTF-8"));//先用base64解密
return new String(cipher.doFinal(decrypted));
} catch (Exception ex) {
}
return null;
}
// DES CBC PKCS5Padding 56位----
// data : 加密字符串(UTF-8) , sKey: 密钥key , sIv : 密钥iv (注意:8位长度)
public static String desEnc(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 8 || data == null) return null;
try {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");//参数解释:"算法/模式/补码方式"
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "DES");//DES算法:通过密钥,增强加密算法强度
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());//CBC模式:通过向量iv,可增加加密算法的强度"16位"
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
byte[] encrypted = cipher.doFinal(data.getBytes());//加密数据
return new String(base64Enc(encrypted)).replaceAll("\r|\n", "");
} catch (Exception e) {
}
return null;
}
// data : 解密字符串(UTF-8) , sKey: 密钥key(8) , sIv : 密钥iv(8)
public static String desDec(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 8) return null;
try {
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "DES");
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
byte[] decrypted = base64Dec(data.getBytes());//先用base64解密
return new String(cipher.doFinal(decrypted));
} catch (Exception ex) {
}
return null;
}
// DES3 CBC PKCS5Padding 168位----
// data : 加密字符串(UTF-8), sKey: 密钥key(24) , sIv : 密钥iv(8)
public static String des3Enc(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 24 || data == null || sIv == null || sIv.length() != 8) return null;
try {
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");//参数解释:"算法/模式/补码方式"
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "DESede");
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
byte[] encrypted = cipher.doFinal(data.getBytes("UTF-8"));//加密数据
return new String(base64Enc(encrypted)).replaceAll("\r|\n", "");
} catch (Exception e) {
}
return null;
}
// data : 解密字符串(UTF-8) , sKey: 密钥key(24) , sIv : 密钥iv(8)
public static String des3Dec(String data, String sKey, String sIv) {
if (sKey == null || sKey.length() != 24 || data == null || sIv == null || sIv.length() != 8) return null;
try {
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec sKeySpec = new SecretKeySpec(sKey.getBytes(), "DESede");
IvParameterSpec iv = new IvParameterSpec(sIv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv);
byte[] decrypted = base64Dec(data.getBytes("UTF-8"));//先用base64解密
return new String(cipher.doFinal(decrypted));
} catch (Exception ex) {
}
return null;
}
}
变更记录:
2022-11-09 :第一版上线时间
2022-12-26 :调整页面布局
** 希望得到你的参与和支持。 如果内容有描述不恰当的地方,请指出。 谢谢!**