package com.by4cloud.platformx.business.utils;
|
|
import cn.hutool.core.lang.Snowflake;
|
import cn.hutool.core.util.IdUtil;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.SecretKeySpec;
|
import java.util.Base64;
|
|
public class AESCFBUtils {
|
|
/**
|
* AES CFB NoPadding 加密
|
* @param src 明文
|
* @param keyWord 密钥字符串
|
* @return Base64 密文
|
*/
|
public static String encrypt(String src, String keyWord) throws Exception {
|
// 1. 处理 Key 和 IV (UTF-8 字节数组)
|
byte[] keyBytes = keyWord.getBytes("UTF-8");
|
|
// AES 要求 Key/IV 长度为 16 字节 (128位)。
|
// CryptoJS 行为:若不足可能补零,若超过可能截断或使用全部(取决于版本/模式内部处理)。
|
// 安全做法:统一处理为 16 字节
|
byte[] fixedKey = new byte[16];
|
System.arraycopy(keyBytes, 0, fixedKey, 0, Math.min(keyBytes.length, 16));
|
|
SecretKeySpec keySpec = new SecretKeySpec(fixedKey, "AES");
|
IvParameterSpec ivSpec = new IvParameterSpec(fixedKey); // IV 等于 Key
|
|
// 2. 初始化 Cipher
|
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
|
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
|
|
// 3. 加密
|
byte[] encryptedBytes = cipher.doFinal(src.getBytes("UTF-8"));
|
|
// 4. 返回 Base64 字符串
|
return Base64.getEncoder().encodeToString(encryptedBytes);
|
}
|
|
public static void main(String[] args) throws Exception {
|
// System.out.println(encrypt("123456","pigxpigxpigxpigx"));
|
// 或者自定义 WorkerId 和 DataCenterId
|
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
|
for (int i = 0; i < 1150; i++) {
|
System.out.println(snowflake.nextId());
|
}
|
}
|
|
}
|