import CryptoJS from 'crypto-js'


export function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(unescape(r[2]))
    return null;
}


export function Encrypt(word) {
    let key = CryptoJS.enc.Base64.parse('HwAjAv7trJl0AP/+5yAVCg==');  //十六位十六进制数作为密钥
    let iv = CryptoJS.enc.Base64.parse('/xgA0AqJ7g0wmdyr/wgA3Q==');   //十六位十
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    return encrypted.ciphertext.toString().toUpperCase();
}
export function Decrypt(word) {
    // console.log(word, "Decrypt")
    if (!word) {
        return word;
    }
    word = (word + '').replace(/\n*$/g, '').replace(/\n/g, ''); //增加这一行，将换行符替换为空
    let key = CryptoJS.enc.Base64.parse('HwAjAv7trJl0AP/+5yAVCg==');  //十六位十六进制数作为密钥
    let iv = CryptoJS.enc.Base64.parse('/xgA0AqJ7g0wmdyr/wgA3Q==');   //十六位十
    let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();

}