Skip to content

Instantly share code, notes, and snippets.

@space4yyy
Last active October 19, 2021 03:36
Show Gist options
  • Save space4yyy/ec2a7ab656082b02d9ee4b182404c6d9 to your computer and use it in GitHub Desktop.
Save space4yyy/ec2a7ab656082b02d9ee4b182404c6d9 to your computer and use it in GitHub Desktop.
postman CryptoJS 常用方法

MD5 摘要

CryptoJS.MD5("数据").toString();

SHA256 摘要

CryptoJS.SHA256("待加密数据").toString();

HmacSHA256 摘要

CryptoJS.HmacSHA256("待加密数据", "秘钥");

base64 加密解密

// 加密 写法1
var utf8Str = CryptoJS.enc.Utf8.parse("待加密字符串");
CryptoJS.enc.Base64.stringify(utf8Str);
// 加密 写法2
CryptoJS.enc.Utf8.parse("待加密字符串").toString(CryptoJS.enc.Base64);

//  解密
CryptoJS.enc.Base64.parse("带解密").toString(CryptoJS.enc.Utf8);

// CryptoJS.enc.Base64.stringify("字符串")  和  "字符串".toString(CryptoJS.enc.Base64)  是相同的含义

AES 简单加密解密

// 加密
CryptoJS.AES.encrypt('待加密字符串', '秘钥').toString();
// 解密
CryptoJS.AES.decrypt('待解密字符串', '秘钥').toString(CryptoJS.enc.Utf8);

自定义 AES 加密解密

const key = CryptoJS.enc.Base64.parse("秘钥");  //这里使用base64 或者 utf8 要看要求,加解密一致即可
   const iv = CryptoJS.enc.Utf8.parse('偏移量');   //十六位十六进制数作为密钥偏移量
   // 加密 1
   function Encrypt(word) {
        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.toString();
    }
// 解密 1
  function Decrypt(word) {
        let decrypt = CryptoJS.AES.decrypt(word, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
        let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        return decryptedStr.toString();
    }
// 加密 2
   function Encrypt(word) {
        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();
    }
// 解密 2
  function Decrypt(word) {
        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();
    }

利用 forgeJS 进行 AES 加密解密

使用远程脚本

if (!pm.globals.has("forgeJS")) {
    pm.sendRequest("http://localhost:8080/forge.js", function (err, res) {
        if (err) {
            console.log(err);
        } else {
            pm.globals.set("forgeJS", res.text());
        }
    })
} else {
   eval(pm.globals.get("forgeJS"))
	// 加解密内容
}

参考资料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment