import CryptoJS from 'crypto-js';

// 假设 PlatformSettings 类的定义
class PlatformSettings {
    static EncryptsKey: string = 'your_encrypts_key';
    static Encoding = {
        GetBytes: (str: string) => CryptoJS.enc.Utf8.parse(str),
        GetString: (bytes: CryptoJS.lib.WordArray) => CryptoJS.enc.Utf8.stringify(bytes)
    };
}

export class CryptTools {
    // 加密KEY
    private static KEY = PlatformSettings.EncryptsKey;

    /**
     * 加密方法
     * @param msg 明文
     * @returns 加密后的字节数组
     */
    public static DesEncryptFixKey(msg: CryptoJS.lib.WordArray): CryptoJS.lib.WordArray {
        const byKey = PlatformSettings.Encoding.GetBytes(this.KEY);
        const IV = CryptoJS.enc.Hex.parse('1234567890ABCDEF');
        const encrypted = CryptoJS.DES.encrypt(msg, byKey, {
            iv: IV,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return CryptoJS.enc.Utf8.parse(encrypted.toString());
    }

    /**
     * 解密方法
     * @param msg 需要解密的字节数组
     * @returns 解密后的字节数组
     */
    public static DesDecryptFixKey(msg: CryptoJS.lib.WordArray): CryptoJS.lib.WordArray {
        const byKey = PlatformSettings.Encoding.GetBytes(this.KEY);
        const IV = CryptoJS.enc.Hex.parse('1234567890ABCDEF');
        const encryptedStr = PlatformSettings.Encoding.GetString(msg);
        const decrypted = CryptoJS.DES.decrypt(encryptedStr, byKey, {
            iv: IV,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return decrypted;
    }
}