import  * as CryptoJS  from './crypto-js-4.2.0/crypto-js';


class AESUtil {
    // AES 加密
    public static AESencrypt(plaintext: string, key: string, iv: string): string {
        const keyUtf8 = CryptoJS.enc.Utf8.parse(key);
        const ivUtf8 = CryptoJS.enc.Utf8.parse(iv);

        const encryptedData = CryptoJS.AES.encrypt(plaintext, keyUtf8, {
            iv: ivUtf8,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7,
        });

        return encryptedData.toString();
    }

    // AES 解密
    public static AESdecrypt(ciphertext: string, key: string, iv: string): string {
        const keyUtf8 = CryptoJS.enc.Utf8.parse(key);
        const ivUtf8 = CryptoJS.enc.Utf8.parse(iv);

        const decryptedData = CryptoJS.AES.decrypt(ciphertext, keyUtf8, {
            iv: ivUtf8,
            padding: CryptoJS.pad.Pkcs7,
        });

        return decryptedData.toString(CryptoJS.enc.Utf8);
    }
}

export default AESUtil;