You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
7.7 KiB
263 lines
7.7 KiB
3 years ago
|
package com.fr.plugin.loginAuth.utils;
|
||
|
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import sun.misc.BASE64Decoder;
|
||
|
import sun.misc.BASE64Encoder;
|
||
|
|
||
|
import javax.crypto.Cipher;
|
||
|
import javax.crypto.SecretKey;
|
||
|
import javax.crypto.SecretKeyFactory;
|
||
|
import javax.crypto.spec.DESKeySpec;
|
||
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
import java.io.IOException;
|
||
|
import java.security.Key;
|
||
|
import java.security.MessageDigest;
|
||
|
import java.security.SecureRandom;
|
||
|
|
||
|
/**
|
||
|
* 加解密工具类
|
||
|
*/
|
||
|
public class CipherUtils {
|
||
|
/**
|
||
|
* 加密
|
||
|
*
|
||
|
* @param datasource
|
||
|
* byte[]
|
||
|
* @param password
|
||
|
* String
|
||
|
* @return byte[]
|
||
|
*/
|
||
|
public static byte[] desEncrypt(byte[] datasource, String password) {
|
||
|
try {
|
||
|
SecureRandom random = new SecureRandom();
|
||
|
DESKeySpec desKey = new DESKeySpec(password.getBytes());
|
||
|
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
|
||
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
||
|
// Cipher对象实际完成加密操作
|
||
|
Cipher cipher = Cipher.getInstance("DES");
|
||
|
// 用密匙初始化Cipher对象
|
||
|
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
|
||
|
// 现在,获取数据并加密
|
||
|
// 正式执行加密操作
|
||
|
return cipher.doFinal(datasource);
|
||
|
} catch (Throwable e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 解密
|
||
|
*
|
||
|
* @param src
|
||
|
* byte[]
|
||
|
* @param password
|
||
|
* String
|
||
|
* @return byte[]
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static byte[] desDecrypt(byte[] src, String password) throws Exception {
|
||
|
// DES算法要求有一个可信任的随机数源
|
||
|
SecureRandom random = new SecureRandom();
|
||
|
// 创建一个DESKeySpec对象
|
||
|
DESKeySpec desKey = new DESKeySpec(password.getBytes("UTF-8"));
|
||
|
// 创建一个密匙工厂
|
||
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
|
||
|
// 将DESKeySpec对象转换成SecretKey对象
|
||
|
SecretKey securekey = keyFactory.generateSecret(desKey);
|
||
|
// Cipher对象实际完成解密操作
|
||
|
Cipher cipher = Cipher.getInstance("DES");
|
||
|
// 用密匙初始化Cipher对象
|
||
|
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
|
||
|
// 真正开始解密操作
|
||
|
return cipher.doFinal(src);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 自定义一个key
|
||
|
**/
|
||
|
public static byte[] getKey(String keyRule) {
|
||
|
Key key = null;
|
||
|
byte[] keyByte = keyRule.getBytes();
|
||
|
// 创建一个空的八位数组,默认情况下为0
|
||
|
byte[] byteTemp = new byte[8];
|
||
|
// 将用户指定的规则转换成八位数组
|
||
|
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
|
||
|
byteTemp[i] = keyByte[i];
|
||
|
}
|
||
|
key = new SecretKeySpec(byteTemp, "DES");
|
||
|
return key.getEncoded();
|
||
|
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将16进制转换为二进制
|
||
|
*
|
||
|
* @param hexStr
|
||
|
* @return
|
||
|
*/
|
||
|
public static byte[] parseHexStr2Byte(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;
|
||
|
}
|
||
|
|
||
|
/***
|
||
|
* 解密数据
|
||
|
* @param decryptString
|
||
|
* @param decryptKey
|
||
|
* @return
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static String decryptDES(String decryptString, String decryptKey) throws Exception {
|
||
|
SecretKeySpec key = new SecretKeySpec(getKey(decryptKey), "DES");
|
||
|
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
|
||
|
cipher.init(Cipher.DECRYPT_MODE, key);
|
||
|
byte decryptedData[] = cipher.doFinal(parseHexStr2Byte(decryptString));
|
||
|
return new String(decryptedData,"utf-8");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* jdksha256加密
|
||
|
* @param str
|
||
|
* @return
|
||
|
*/
|
||
|
public static String jdksha256(String str)
|
||
|
{
|
||
|
String sha256Str = "";
|
||
|
|
||
|
try {
|
||
|
MessageDigest sha256Deget = MessageDigest.getInstance("SHA-256");
|
||
|
byte[] sha256Encode = sha256Deget.digest(str.getBytes());
|
||
|
sha256Str = ByteToHexStr(sha256Encode);
|
||
|
}catch (Exception e){
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:SHA256加密异常:"+e.getMessage());
|
||
|
}
|
||
|
|
||
|
return sha256Str;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* byte数组转16进制字符串
|
||
|
* @param bytes
|
||
|
* @return
|
||
|
*/
|
||
|
private static String ByteToHexStr(byte[] bytes)
|
||
|
{
|
||
|
String hexStr = "";
|
||
|
|
||
|
for(int i =0;i<bytes.length;i++)
|
||
|
{
|
||
|
int temp = bytes[i] & 0xff;
|
||
|
String tempHex = Integer.toHexString(temp);
|
||
|
if(tempHex.length() < 2)
|
||
|
{
|
||
|
hexStr += "0"+tempHex;
|
||
|
}
|
||
|
else {
|
||
|
hexStr += tempHex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return hexStr;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* base64加密
|
||
|
* @param key
|
||
|
* @return
|
||
|
*/
|
||
|
public static String base64Encode(String key){
|
||
|
return (new BASE64Encoder()).encodeBuffer(key.getBytes());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* base64解密
|
||
|
* @param key
|
||
|
* @return
|
||
|
*/
|
||
|
public static String base64Decode(String key){
|
||
|
String result = "";
|
||
|
try {
|
||
|
result = new String((new BASE64Decoder()).decodeBuffer(key));
|
||
|
} catch (IOException e) {
|
||
|
FineLoggerFactory.getLogger().info("FRLOG:BASE64解密异常:"+e.getMessage());
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
public static String getBase64DecodeStr(String str){
|
||
|
if(str == null || str.isEmpty()){
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
String result = base64Decode(str);
|
||
|
return result.contains("base64")?result:str;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 是否被base64加密过
|
||
|
* @param str
|
||
|
* @return
|
||
|
*/
|
||
|
public static boolean isBase64(String str) {
|
||
|
if (str == null || str.trim().length() == 0) {
|
||
|
return false;
|
||
|
}
|
||
|
else {
|
||
|
if (str.length() % 4 != 0) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
char[] strChars = str.toCharArray();
|
||
|
|
||
|
for (char c:strChars) {
|
||
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
||
|
|| c == '+' || c == '/' || c == '=') {
|
||
|
continue;
|
||
|
}
|
||
|
else {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
public static void main(String[] args) throws Exception {
|
||
|
// String key = "z2bse2zh";
|
||
|
// String key = "ajshcjzj";
|
||
|
// String str = "男";
|
||
|
////
|
||
|
// byte[] a =desEncrypt(str.getBytes(),key);
|
||
|
//// System.out.println(new String(a));
|
||
|
// String a1 = new BASE64Encoder().encodeBuffer(a);
|
||
|
////
|
||
|
// System.out.println(a1);
|
||
|
////
|
||
|
//// byte[] b = desDecrypt(a,key);
|
||
|
//// System.out.println(new String(b));
|
||
|
//
|
||
|
// String key2 = "rdTK5iLsDFw=";
|
||
|
// byte[] b2 = desDecrypt(new BASE64Decoder().decodeBuffer(key2),"ajshcjzj");
|
||
|
// System.out.println(new String(b2));
|
||
|
// //密钥
|
||
|
// String key = "z2bse2zh";
|
||
|
// //解密数据
|
||
|
// String encryDate = "DF2DF0F837C38A1233A8B1255B0532E36E9D29052873B7F8707936C025E2FE368B0A8919A93C8869";
|
||
|
// String a = "ED33AD65CDBAEF49";
|
||
|
// String result = decryptDES(a, key);
|
||
|
//
|
||
|
// System.out.println("result:" + result);
|
||
|
// System.out.println("result2:"+new String(decrypt(result.getBytes(),key)));
|
||
|
}
|
||
|
}
|