package shop.alien.util.encryption;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * Jasypt 统一加解密工具类
 */
public class JasyptEncryptorUtil {

    /**
     * 手动加密
     * @param salt 盐值
     * @param text 待加密文本
     * @return 密文
     */
    public static String encode(String salt, String text) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setAlgorithm(JasyptAutoConfiguration.ALGORITHM);
        encryptor.setPassword(salt); // 直接设置盐值，不经过 Config 对象
        encryptor.setIvGenerator(new org.jasypt.iv.NoIvGenerator());
        return encryptor.encrypt(text);
    }

    public static String decode(String salt, String cipherText) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setAlgorithm(JasyptAutoConfiguration.ALGORITHM);
        encryptor.setPassword(salt); // 直接设置盐值
        encryptor.setIvGenerator(new org.jasypt.iv.NoIvGenerator());
        return encryptor.decrypt(cipherText);
    }
}
