|
@ -5,6 +5,9 @@ import org.apache.commons.codec.binary.Base64;
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.KeyGenerator;
|
|
import javax.crypto.KeyGenerator;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 前后端数据传输加密工具类
|
|
* 前后端数据传输加密工具类
|
|
@ -60,25 +63,46 @@ public class AesEncryptUtils {
|
|
|
public static String decrypt(String encryptStr) throws Exception {
|
|
public static String decrypt(String encryptStr) throws Exception {
|
|
|
return decrypt(encryptStr, KEY);
|
|
return decrypt(encryptStr, KEY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
/*Map map=new HashMap<String,String>();
|
|
|
|
|
map.put("idcard","350322198812052545");
|
|
|
|
|
map.put("price","1");
|
|
|
|
|
map.put("type","1");*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String content = "{\"idcard\":\"350322198812052545\",\"price\":\"1\",\"type\":\"1\"}";
|
|
|
|
|
System.out.println("加密前:" + content);
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
double totalAmount = 1; // 总金额
|
|
|
|
|
int count = 1; // 红包数量
|
|
|
|
|
double minAmount = 1.0; // 最小红包金额
|
|
|
|
|
double maxAmount = 3.0; // 最大红包金额
|
|
|
|
|
|
|
|
|
|
List<Double> redPackets = distributeRedPackets(totalAmount, count, minAmount, maxAmount);
|
|
|
|
|
for (double amount : redPackets) {
|
|
|
|
|
System.out.println(String.format("红包金额: %.2f", amount));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<Double> distributeRedPackets(double totalAmount, int count, double minAmount, double maxAmount) {
|
|
|
|
|
List<Double> redPackets = new ArrayList<>();
|
|
|
|
|
Random random = new Random();
|
|
|
|
|
double remainingAmount = totalAmount;
|
|
|
|
|
double perMaxAmount = (totalAmount - count * minAmount) / count; // 计算除去所有最小值后,每个红包最多可以有多少金额
|
|
|
|
|
perMaxAmount = Math.min(perMaxAmount, maxAmount - minAmount); // 取perMaxAmount和maxAmount-minAmount的最小值,防止超出最大限制
|
|
|
|
|
double perMinAmount = minAmount; // 每个红包的最小金额
|
|
|
|
|
|
|
|
String encrypt = encrypt(content, KEY);
|
|
|
|
|
System.out.println("加密后:" + encrypt);
|
|
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
// 计算当前红包的金额范围
|
|
|
|
|
double currentMax = Math.min(perMaxAmount + perMinAmount, maxAmount); // 当前红包的最大值(不能超过设定的最大值)
|
|
|
|
|
double currentMin = perMinAmount; // 当前红包的最小值
|
|
|
|
|
double currentAmount = currentMin + random.nextDouble() * (currentMax - currentMin); // 在当前范围内随机分配金额
|
|
|
|
|
redPackets.add(currentAmount);
|
|
|
|
|
remainingAmount -= currentAmount; // 减去已分配的金额
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* WibZYHSxGPdkw8z37G/kyZLO6PwBxDzQBbTZig+m/gBTsvLSoVgp2fgf4fUgenTMfTxbZMbGfrQI969ZpS2Esg==
|
|
|
|
|
*/
|
|
|
|
|
String decrypt = decrypt(encrypt, KEY);
|
|
|
|
|
System.out.println("解密后:" + decrypt);
|
|
|
|
|
|
|
// 如果还有剩余金额,则重新分配给前面的红包,直到用完所有剩余金额或达到设定的最大值限制
|
|
|
|
|
if (remainingAmount > 0) {
|
|
|
|
|
for (int i = 0; i < count && remainingAmount > 0; i++) {
|
|
|
|
|
double additionalAmount = Math.min(remainingAmount, maxAmount - redPackets.get(i)); // 最多只能增加到maxAmount
|
|
|
|
|
redPackets.set(i, redPackets.get(i) + additionalAmount);
|
|
|
|
|
remainingAmount -= additionalAmount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return redPackets;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|