shiyunteng
6 天以前 7ffef0059ddf3d4a82de4a4a8999b4b2429fcda6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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());
        }
    }
 
}