135 lines
4.4 KiB
Java
135 lines
4.4 KiB
Java
package com.zhgd.xmgl.util;
|
||
|
||
|
||
import com.zhgd.xmgl.entity.vo.NanchangHousingBean;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.apache.commons.codec.binary.Base64;
|
||
import org.apache.commons.lang.StringUtils;
|
||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||
|
||
|
||
import javax.crypto.Cipher;
|
||
import javax.crypto.spec.IvParameterSpec;
|
||
import javax.crypto.spec.SecretKeySpec;
|
||
import javax.validation.constraints.NotNull;
|
||
import java.io.UnsupportedEncodingException;
|
||
import java.net.URLEncoder;
|
||
import java.security.Security;
|
||
import java.security.spec.AlgorithmParameterSpec;
|
||
import java.util.*;
|
||
|
||
/**
|
||
* @program: wisdomSite
|
||
* @description: 南昌住建局工具类
|
||
* @author: Mr.Peng
|
||
* @create: 2021-07-05 17:10
|
||
**/
|
||
|
||
@Slf4j
|
||
public class NanchangHousingUtils {
|
||
|
||
private static final String CHARSET_NAME = "UTF-8";
|
||
private static final String AES_NAME = "AES";
|
||
public static final String ALGORITHM = "AES/CBC/PKCS7Padding";
|
||
|
||
static {
|
||
Security.addProvider(new BouncyCastleProvider());
|
||
}
|
||
|
||
/**
|
||
* 加密
|
||
*
|
||
* @param content
|
||
* @param key
|
||
* @return
|
||
*/
|
||
public static String encrypt(@NotNull String content, @NotNull String key) {
|
||
byte[] result = null;
|
||
try {
|
||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET_NAME), AES_NAME);
|
||
AlgorithmParameterSpec paramSpec = new IvParameterSpec(
|
||
subBytes(key.getBytes(CHARSET_NAME)));
|
||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, paramSpec);
|
||
result = cipher.doFinal(content.getBytes(CHARSET_NAME));
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
return Base64.encodeBase64String(result);
|
||
}
|
||
|
||
/**
|
||
* 解密
|
||
*
|
||
* @param content
|
||
* @param key
|
||
* @return
|
||
*/
|
||
public static String decrypt(@NotNull String content, @NotNull String key) {
|
||
try {
|
||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(CHARSET_NAME), AES_NAME);
|
||
AlgorithmParameterSpec paramSpec = new IvParameterSpec(
|
||
subBytes(key.getBytes(CHARSET_NAME)));
|
||
cipher.init(Cipher.DECRYPT_MODE, keySpec, paramSpec);
|
||
return new String(cipher.doFinal(Base64.decodeBase64(content)), CHARSET_NAME);
|
||
} catch (Exception ex) {
|
||
ex.printStackTrace();
|
||
}
|
||
return StringUtils.EMPTY;
|
||
}
|
||
|
||
/**
|
||
* 从一个byte[]数组中截取一部分
|
||
*
|
||
* @param src
|
||
* @return
|
||
*/
|
||
public static byte[] subBytes(byte[] src) {
|
||
if (src.length < 16) {
|
||
throw new RuntimeException("无法从Key中获取偏移量!");
|
||
}
|
||
byte[] bs = new byte[16];
|
||
for (int i = 0; i < 16; i++) {
|
||
bs[i] = src[i];
|
||
}
|
||
return bs;
|
||
}
|
||
|
||
public static String getSing(NanchangHousingBean bean) {
|
||
//listMap示例
|
||
List<Map> listMap = new ArrayList<>();
|
||
/*Map<String, String> testMap = new HashMap<>(16);
|
||
testMap.put("name", "张三");
|
||
testMap.put("code", "123456");*/
|
||
listMap.add(bean.getData());
|
||
//加密秘钥
|
||
String secret = bean.getSecret();
|
||
String appKey = bean.getAppKey();
|
||
//拼装字符串
|
||
StringBuilder sb = new StringBuilder();
|
||
TreeMap<String, String> map = new TreeMap<String, String>(listMap.get(0));
|
||
for (Map.Entry<String, String> e : map.entrySet()) {
|
||
if (StringUtils.isNotBlank(e.getValue()) && !"headImage".equals(e.getKey()) && !"image".equals(e.getKey())) {
|
||
sb.append(e.getKey()).append("=").append(e.getValue()).append("&");
|
||
}
|
||
}
|
||
sb.append("appKey=").append(appKey);
|
||
//生成签名
|
||
String signCheck = NanchangHousingUtils.encrypt(sb.toString(), secret).toUpperCase();
|
||
try {
|
||
//转义
|
||
signCheck = URLEncoder.encode(signCheck, "UTF-8")
|
||
.replaceAll("\\+", "%20")
|
||
.replaceAll("\\!", "%21")
|
||
.replaceAll("\\'", "%27")
|
||
.replaceAll("\\(", "%28")
|
||
.replaceAll("\\)", "%29")
|
||
.replaceAll("\\~", "%7E");
|
||
} catch (UnsupportedEncodingException e) {
|
||
log.error("error:", e);
|
||
}
|
||
return signCheck;
|
||
}
|
||
}
|