package org.dromara.system.api.utils;

import cn.hutool.core.util.HexUtil;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @author lilemy
 * @date 2025/6/25 10:57
 */
@Slf4j
@Component
public class IdCardEncryptorUtil {

    @Value("${id-card.encrypt-key}")
    private String encryptKeyHex;

    private AES aes;

    @PostConstruct
    public void init() {
        byte[] keyBytes = HexUtil.decodeHex(encryptKeyHex);
        this.aes = SecureUtil.aes(keyBytes);
        log.info("身份证 AES 加解密工具初始化成功");
    }

    /**
     * 加密
     *
     * @param idCard 身份证号码
     * @return 加密后的身份证号码
     */
    public String encrypt(String idCard) {
        return aes.encryptBase64(idCard);
    }

    /**
     * 解密
     *
     * @param encrypted 密文
     * @return 解密后的身份证号码
     */
    public String decrypt(String encrypted) {
        if (encrypted == null) {
            return null;
        }
        return aes.decryptStr(encrypted);
    }
}
