前端加密

1、md5加密:前端可以加密,不可以解密
2、crypto加密: 前端可加密、解密常用于“记住密码”, 存cookie

import CryptoJS from "crypto-js";

//设置cookie
    setCookie(portId, psw, exdays) {
      // Encrypt,加密账号密码
      const userName = CryptoJS.AES.encrypt(
        portId+'',
        "secretkey123"
      ).toString();
      let cipherPsw = CryptoJS.AES.encrypt(psw+'', "secretkey123").toString();
      // window.console.log(userName+'/'+cipherPsw)//打印一下看看有没有加密成功
      let exdate = new Date(); //获取时间
      exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
      //字符串拼接cookie,为什么这里用了==,因为加密后的字符串也有个=号,影响下面getcookie的字符串切割,你也可以使用更炫酷的符号。
      window.document.cookie =
        "userName" +
        "==" +
        userName +
        ";path=/;expires=" +
        exdate.toGMTString();
      window.document.cookie =
        "password" +
        "==" +
        cipherPsw +
        ";path=/;expires=" +
        exdate.toGMTString();
    },

     //读取cookie
    getCookie () {
      if (document.cookie && document.cookie.length > 0) {
        let arr = document.cookie.split("; "); //这里显示的格式请根据自己的代码更改
        for (let i = 0; i < arr.length; i++) {
          let arr2 = arr[i].split("=="); //根据==切割
          //判断查找相对应的值
          if (arr2[0] == "userName") {
            // Decrypt,将解密后的内容赋值给账号
            let bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
            this.userName = bytes.toString(CryptoJS.enc.Utf8) || '';
          } else if (arr2[0] == "password") {
            // Decrypt,将解密后的内容赋值给密码
            let bytes = CryptoJS.AES.decrypt(arr2[1], "secretkey123");
            this.password = bytes.toString(CryptoJS.enc.Utf8) || '';
          }
        }
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经常看到有人要找AES-GCM-128  这个算法加解密 网上相关的文档也较少其中在telegram登录首页就在使用该算法 应该让不少哥们头疼 其实这个加密常见于浏览器内置接口window.crypto.subtle 该接口不仅支持该类型的加密 且支持非常多的算法加密如RSA DES  等等  这里就演示AES-GCM-128 这个类型 crypto-AES-GCM-128调用例子 function ___crypto__test(keyData, iv, data) {     const format = "raw",         // keyData = new Uint8Array([23, 113, 57, 6, 35, -69, -60, 98, 84, -17, -125, -49, 18, 6, -92, 32]),         algorithm = "AES-GCM",         extractable = true,         usages = ["encrypt", "decrypt"];     // iv = new Uint8Array([47, 46, 123, 78, 36, 14, 109, 50, 121, 64, 11, 38]);     window.crypto.subtle.importKey(         format,         keyData,         algorithm,         extractable, usages     ).then(key =gt; {         window.crypto.subtle.encrypt({                 name: algorithm,                 iv: iv             },             key,             data         ).then(result =gt; {             console.log(Array.from(new Uint8Array((result))))         })     }) } console.log(___crypto__test(             new Uint8Array([23, 113, 57, 6, 35, -69, -60, 98, 84, -17, -125, -49, 18, 6, -92, 32]),                 new Uint8Array([47, 46, 123, 78, 36, 14, 109, 50, 121, 64, 11, 38]),             new Uint8Array([50, 49, 48]) )) crypto主要相关接口介绍 crypto.subtle.importKey const result = crypto.subtle.importKey(     format,     keyData,     algorithm,     extractable,     usages ); format  是一个字符串,描述要导入的密钥的数据格式。可以是以下之一:----------raw:原始格式。----------pkcs8:PKCS#8格式。----------spki:SubjectPublicKeyInfo格式。----------jwk:JSON Web密钥格式。 - keyData 是ArrayBuffer,TypedArray,a DataView或JSONWebKey包含给定格式的键的对象。 - algorithm  是一个字典对象,用于定义要导入的密钥的类型并提供额外的算法特定参数。对于RSASSA-PKCS1-v1_5,  RSA-PSS或  RSA-OAEP:传递RsaHashedImportParams对象。对于ECDSA或ECDH:传递  EcKeyImportParams对象。对于HMAC:传递一个HmacImportParams对象。对于AES-CTR,AES-CBC,AES-GCM或AES-KW:传递标识算法的字符串或形式为的对象{ "name": ALGORITHM },其中ALGORITHM 是算法的名称。对于PBKDF2  :传递字符串PBKDF2。 - extractable 是Boolean表明它是否将有可能使用到导出密钥SubtleCrypto.exportKey()或SubtleCrypto.wrapKey()。 - ke

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值