评论

CryptoJS 加解密使用示例

CryptoJS 加解密使用示例


// SHA1 加密
 
var value = "123456";
 
var wordArray = CryptoJS.SHA1(value);
 
var str = wordArray.toString(CryptoJS.enc.Hex);



// HmacSHA1加密
 
var message = "message";
 
var key = "key";
 
var wordArray = CryptoJS.HmacSHA1(message, key);
 
var str = wordArray.toString(CryptoJS.enc.Hex);


// md5 加密
 
var md5 = CryptoJS.MD5("md5").toString();


// AES 加解密 开始
 
 
 
/**
 
 * //AES 解密方法
 
 * word 字符串
 
 */
 
const AES_JIA = function (word, key, iv) {
 
 
 
    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.ECB, padding: CryptoJS.pad.Pkcs7 });
 
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
 
 
 
    return decryptedStr.toString();
 
}
 
/**
 
 * //AES 加密方法
 
 * word 字符串
 
 */
 
 
 
const AES_JIE = function (word, key, iv) {
 
    let srcs = CryptoJS.enc.Utf8.parse(word);
 
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
 
        iv: iv,
 
        mode: CryptoJS.mode.ECB,
 
        padding: CryptoJS.pad.Pkcs7
 
    });
 
    return encrypted.ciphertext.toString().toUpperCase();
 
}
 
 
 
const word = "字符串格式"; // 字符串格式
 
const key = CryptoJS.enc.Utf8.parse("1234567890123456"); //十六位十六进制数作为密钥 ,十六位,十六位,不要 误以为 1234567890123456  == 123  是行得通的 字符长度16不等于 3,除非 key  = 123
 
const iv = CryptoJS.enc.Utf8.parse(''); //十六位十六进制数作为密钥偏移量
 
 
 
var ctext = AES_JIA(word, key, iv);
 
console.log("ctext=>", ctext);   // AES 加密
 
 
 
var ptext = AES_JIE(ctext, key, iv);
 
console.log("ptext=>", ptext);  // AES 解密
 
 
 
// AES 加解密 结束


//DES 加密
function DES_JIA(message, key, iv) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        iv: iv,
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}
 
//DES 解密
function DES_JIE(ciphertext, key, iv) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    // direct decrypt ciphertext
    var decrypted = CryptoJS.DES.decrypt({
        ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
    }, keyHex, {
            iv: iv,
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        });
    return decrypted.toString(CryptoJS.enc.Utf8);
}
 
var des_text = DES_JIA(word, key, iv);
console.log("des_text=>", des_text);   // des 加密
 
var ntext = DES_JIE(des_text, key, iv);
console.log("ntext=>", ntext);  // des 解密


调试(SHA1 加密)图片示例:




参考资料:

https://cryptojs.gitbook.io/docs/


https://www.bootcdn.cn/crypto-js/


最后一次编辑于  2019-05-29  
点赞 5
收藏
评论

7 个评论

  • 同步
    同步
    2023-08-03

    2023-08-03
    赞同
    回复
  • Yellow
    Yellow
    2023-06-09

    太感谢了

    2023-06-09
    赞同
    回复 1
    • 同步
      同步
      2023-06-14
      不客气
      2023-06-14
      回复
  • 听凭风引
    听凭风引
    2023-04-15

    crypto-js 这个js文件官方下载后导入到本地,但是需要导出呀,朋友你是怎么导出的。

    2023-04-15
    赞同
    回复 2
    • 同步
      同步
      2023-04-18
      2023-04-18
      回复
    • 同步
      同步
      2023-04-18回复同步
      直接引用,拿到 CryptoJS  变量。
      2023-04-18
      回复
  • 韦不吕
    韦不吕
    2020-04-19

    我直接在小程序使用npm install crypto-js,然后在小程序端引用出错:

    module "pages/order/crypto-js" is not defined

    2020-04-19
    赞同
    回复 4
    • 同步
      同步
      2020-04-20
      我是从
      https://www.bootcdn.cn/crypto-js/ 网址下载
      小程序引用
      const CryptoJS = require("../../utils/crypto-js.js");


      并非使用模块引入方式
      2020-04-20
      回复
    • 同步
      同步
      2020-04-20
      https://developers.weixin.qq.com/s/MU6E6KmY7Kg8


      做了个简单代码片段,小程序引入模块的方式是,先在项目中 npm install crypto-js , npm init ,npm install  ,然后在工具中构建,构建成功后,才能正常引用,但引入成功后怎么加解密,需要你自己尝试了,我也没试过这块
      2020-04-20
      1
      回复
    • 同步
      同步
      2020-04-20
      2020-04-20
      回复
    • 韦不吕
      韦不吕
      2020-04-20回复同步
      谢谢。重复你的步骤即可解决。
      2020-04-20
      回复
  • 韦不吕
    韦不吕
    2020-04-19

    请教一下,在小程序端如何引用crypto-js模块?

    2020-04-19
    赞同
    回复
  • 牛
    2019-12-13

    为啥我是用ecb模式加密出来的结果和在线加密的不一样

    const mi = new crypto.AES().encrypt('布包云', "BubaoCloud", {


    mode: crypto.Mode.ECB,


    padding: crypto.Padding.Pkcs7


    });


    2019-12-13
    赞同
    回复
  • ㋡
    2019-09-05

    有没有碰到AES解密 后台报错 CryptoJS.AES.decrypt这步   CryptoJS.AES

    undefined is not an object (evaluating 'e.ciphertext');at api request success callback function decrypt@https://utils/crypto-js.js:3969:72

    2019-09-05
    赞同
    回复 1
    • 同步
      同步
      2019-09-05

      之前遇到过类似报错,但不完全相同,修改规则(如下代码)后,通过验证了,具体的你跟你们后端调试的时候看看是怎么样的,变通下。

      var CryptoJS = require("./crypto-js");
      /**
       * //AES 解密方法
       * word 字符串
       * key 秘钥
       */
      const AES_JIE = function (word,  _key,_iv) {
          let srcs = word;
          let decrypt = CryptoJS.AES.decrypt(srcs, _key, {
            iv: _iv,
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
          });
          let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
          return decryptedStr.toString();
        }
        /**
         * //AES 加密方法
         * word 字符串
         * key 秘钥
         */
        const AES_JIA = function (word, _key,_iv) {
         
          let srcs = CryptoJS.enc.Utf8.parse(word);
          
          let encrypted = CryptoJS.AES.encrypt(srcs, _key, {
            iv: _iv,
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
          });
          // return encrypted.ciphertext.toString().toUpperCase();
          let encryptedHexStr = CryptoJS.enc.Hex.parse(encrypted.ciphertext.toString().toUpperCase());
         
          return CryptoJS.enc.Base64.stringify(encryptedHexStr);
        }
       
      // 示例开始
      const word = "字符串格式"; // 字符串格式
        
      const key = CryptoJS.enc.Utf8.parse("1234567890123456"); //十六位十六进制数作为密钥 ,十六位,十六位,不要 误以为 1234567890123456  == 123  是行得通的 字符长度16不等于 3,除非 key  = 123
        
      const iv = CryptoJS.enc.Utf8.parse(''); //十六位十六进制数作为密钥偏移量
       
       
       
      var ctext = AES_JIA(word, key, iv);
        
      console.log("ctext=>", ctext);   // 示例输出 AES 加密
        
        
        
      var ptext = AES_JIE(ctext, key, iv);
        
      console.log("ptext=>", ptext);  //示例输出 AES 解密


      2019-09-05
      回复
登录 后发表内容