Merge remote-tracking branch 'origin/cc_20230520_partner' into cc_20230520_partner

This commit is contained in:
zhangchenbiao
2023-06-19 19:54:53 +08:00
41 changed files with 916 additions and 156 deletions

View File

@@ -120,4 +120,6 @@ public class CommonConstants {
public static final String TRANSFER = "transfer";
public static final String FIX_MOBILE_OPENID_TEST = "HSAY5531DA7";
public static final String FIX_MOBILE_OPENID_ONLINE = "HSAY4AF322E";
}

View File

@@ -0,0 +1,139 @@
package com.cool.store.utils;
import com.cool.store.exception.ServiceException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* https://cloud.tencent.com/developer/article/1823249
* 前后端约定
*/
public class AESDecryptor {
private static String Algorithm = "AES";
private static String AlgorithmProvider = "AES/CBC/PKCS5Padding"; //算法/模式/补码方式
public static byte[] generatorKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(Algorithm);
keyGenerator.init(256);//默认128获得无政策权限后可为192或256
SecretKey secretKey = keyGenerator.generateKey();
return secretKey.getEncoded();
}
public static IvParameterSpec getIv(byte[] ivstr) throws UnsupportedEncodingException {
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivstr);
return ivParameterSpec;
}
public static String encrypt(String src, String keystr, IvParameterSpec iv) throws
NoSuchAlgorithmException,
NoSuchPaddingException,
InvalidKeyException,
IllegalBlockSizeException,
BadPaddingException,
UnsupportedEncodingException,
InvalidAlgorithmParameterException {
byte[] key = keystr.getBytes("utf-8");
SecretKey secretKey = new SecretKeySpec(key, Algorithm);
IvParameterSpec ivParameterSpec = iv;
Cipher cipher = Cipher.getInstance(AlgorithmProvider);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] cipherBytes = cipher.doFinal(src.getBytes(Charset.forName("utf-8")));
return byteToHexString(cipherBytes);
}
public static String decrypt(String src, String keystr) {
try {
byte[] key = keystr.getBytes("utf-8");
SecretKey secretKey = new SecretKeySpec(key, Algorithm);
IvParameterSpec ivParameterSpec = getIv(Arrays.copyOfRange(keystr.getBytes("utf-8"), 0, 16));
Cipher cipher = Cipher.getInstance(AlgorithmProvider);
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] hexBytes = hexStringToBytes(src);
byte[] plainBytes = cipher.doFinal(hexBytes);
return new String(plainBytes, "utf-8");
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
}
/**
* 将byte转换为16进制字符串
*
* @param src
* @return
*/
public static String byteToHexString(byte[] src) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xff;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
sb.append("0");
}
sb.append(hv);
}
return sb.toString();
}
/**
* 将16进制字符串装换为byte数组
*
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] b = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
b[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return b;
}
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static void main(String[] args) {
try {
/* // 密钥必须是16的倍数
String keystr = "0123456789ABCDEF";
String ivstr = "0123456789101112";
String src = "Hello World";
System.out.println("密钥:" + keystr);
System.out.println("偏移量:" + ivstr);
System.out.println("原字符串:" + src);
IvParameterSpec iv = getIv(ivstr);
String enc = encrypt(src, keystr, iv);
System.out.println("加密:" + enc);
*/
String enc = "38395651e391c4b8ca327c4742b7f52f";
String keystr = "77fea013c3a6459685b83c21a2fc3411";
String ivstr = "77fea013c3a64596";
// IvParameterSpec iv = getIv(ivstr);
String dec = decrypt(enc, keystr);
System.out.println("解密:" + dec);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -2,7 +2,6 @@ package com.cool.store.utils;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
@@ -12,8 +11,7 @@ import com.lowagie.text.pdf.PdfWriter;
import java.awt.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
//生成资格面试通过函的工具
public class PassLetterUtils {
@@ -26,7 +24,7 @@ public class PassLetterUtils {
* @param passCode 通过函编码
* @param passTime 审批通过时间
*/
public static ByteArrayOutputStream genPassLetter(String partnerName, String passCode, String verifyCity, DateTime passTime) {
public static ByteArrayOutputStream genPassLetter(String partnerName, String passCode, String verifyCity, Date passTime) {
String passTimeStr = DateUtil.format(passTime, "yyyy年MM月dd日");
Document document = new Document();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -65,7 +63,7 @@ public class PassLetterUtils {
* 生成 passCode 的方法,拆分出来方便单独获取 passCode
* @return
*/
public static String genPassCode(DateTime passTime) {
public static String genPassCode(Date passTime) {
String randomNum = RandomUtil.randomNumbers(5);
String passCode = "HSAY" + DateUtil.format(passTime, "yyMMdd") + "-" + randomNum;
return passCode;