import CryptoJS from "crypto-js";

export function encryptPlaintext(key: string, plaintext: string) {
    const obj = {
        plaintext,
        mark: true,
    }
    return AES_ECB_ENCRYPT(JSON.stringify(obj), key);
}

export function decryptCiphertext(key: string, ciphertext: string) {
    const str = AES_ECB_DECRYPT(ciphertext, key);
    try {
        const obj = JSON.parse(str)
        if (obj.mark === true) {
            return obj.plaintext;
        } else {
            throw new Error('密码错误')
        }
    } catch (err) {
        throw new Error('密码错误')
    }
}



/**
 * AES-256-ECB对称加密
 * @param text {string} 要加密的明文
 * @param secretKey {string} 密钥，43位随机大小写与数字
 * @returns {string} 加密后的密文，Base64格式
 */
function AES_ECB_ENCRYPT(text: string, secretKey: string) {
    const keyHex = CryptoJS.enc.Base64.parse(secretKey);
    const messageHex = CryptoJS.enc.Utf8.parse(text);
    const encrypted = CryptoJS.AES.encrypt(messageHex, keyHex, {
        "mode": CryptoJS.mode.ECB,
        "padding": CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}


/**
 * AES-256-ECB对称解密
 * @param textBase64 {string} 要解密的密文，Base64格式
 * @param secretKey {string} 密钥，43位随机大小写与数字
 * @returns {string} 解密后的明文
 */
function AES_ECB_DECRYPT(textBase64: string, secretKey: string) {
    const keyHex = CryptoJS.enc.Base64.parse(secretKey);
    const decrypt = CryptoJS.AES.decrypt(textBase64, keyHex, {
        "mode": CryptoJS.mode.ECB,
        "padding": CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt);
}