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.
105 lines
3.5 KiB
105 lines
3.5 KiB
3 years ago
|
package com.eco.plugin.xxx.bwjtsso.utils;
|
||
|
|
||
|
|
||
|
import org.apache.commons.codec.binary.Base64;
|
||
|
|
||
|
import javax.crypto.Cipher;
|
||
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
import java.net.URLEncoder;
|
||
|
import java.util.Date;
|
||
|
|
||
|
public class AesUtils {
|
||
|
/**
|
||
|
* 加密
|
||
|
* @param sSrc 需要加密的字符串
|
||
|
* @param sKey 加密钥匙
|
||
|
* @return 加密后的字符串
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static String encrypt(String sSrc, String sKey) throws Exception {
|
||
|
|
||
|
try{
|
||
|
if (null == sKey){
|
||
|
System.out.print("Key为空null");
|
||
|
return null;
|
||
|
}
|
||
|
// 判断Key是否为16位
|
||
|
if (sKey.length() != 16){
|
||
|
System.out.print("Key长度不是16位");
|
||
|
return null;
|
||
|
}
|
||
|
// 判断Key是否为固定值
|
||
|
if (!sKey.equals("6ay4dlwd4enjbf90")){
|
||
|
System.out.print("Key值不对");
|
||
|
return null;
|
||
|
}
|
||
|
byte[] raw = sKey.getBytes("utf-8");
|
||
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
|
||
|
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
|
||
|
Date now = new Date();
|
||
|
long time = now.getTime();
|
||
|
sSrc = sSrc + "_" + time;
|
||
|
System.out.println("加密前字符串是:"+sSrc);
|
||
|
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
|
||
|
return EncryptUtils.base64Encode(encrypted);
|
||
|
}catch (Exception ex){
|
||
|
System.out.println(ex.toString());
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 解密
|
||
|
* @param sSrc 需要解密的字符串
|
||
|
* @param sKey 解密钥匙
|
||
|
* @return 解密后的字符串
|
||
|
* @throws Exception
|
||
|
*/
|
||
|
public static String decrypt(String sSrc, String sKey){
|
||
|
try{
|
||
|
// 判断Key是否正确
|
||
|
if (null == sKey){
|
||
|
FRUtils.FRLogInfo("Key为空null");
|
||
|
return null;
|
||
|
}
|
||
|
// 判断Key是否为16位
|
||
|
if (sKey.length() != 16){
|
||
|
FRUtils.FRLogInfo("Key长度不是16位");
|
||
|
return null;
|
||
|
}
|
||
|
// // 判断Key是否为固定值
|
||
|
// if (!sKey.equals("6ay4dlwd4enjbf90")){
|
||
|
// FRUtils.FRLogInfo("");
|
||
|
// System.out.print("Key值不对");
|
||
|
// return null;
|
||
|
// }
|
||
|
byte[] raw = sKey.getBytes("utf-8");
|
||
|
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
||
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||
|
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|
||
|
byte[] encrypted1 =EncryptUtils.base64DecodeB(sSrc);// 先用base64解密
|
||
|
byte[] original = cipher.doFinal(encrypted1);
|
||
|
String originalString = new String(original, "utf-8");
|
||
|
String []strArr = originalString.split("_");
|
||
|
String timeStr = strArr[1];
|
||
|
long timeOld = Long.parseLong(timeStr);
|
||
|
Date now = new Date();
|
||
|
long timeNow = now.getTime();
|
||
|
long minute = (timeNow - timeOld)/1000/60;
|
||
|
if(minute <= 2) {// 解密在两分钟内有效
|
||
|
return strArr[0];
|
||
|
}else {
|
||
|
FRUtils.FRLogInfo("当前检验已过期!!!");
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
}catch (Exception ex){
|
||
|
FRUtils.FRLogInfo("exception : "+ex.getMessage());
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|