utils:crypto-js的基本使用和(加密/解密)功能封装

一、基本使用

1. 资源下载

crypto-js

2. 目录结构

在这里插入图片描述

3. 代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="crypto-js/crypto-js.js"></script>
<script type="text/javascript">
function encryptData(){
	var str=document.getElementById('id0').value;
	var key=document.getElementById('id7').value;
	var iv=document.getElementById('id9').value;
	document.getElementById('id2').value=encrypt(key,iv,JSON.stringify(str));
}
function encodeURL1(){
	var str=document.getElementById('id0').value;
	document.getElementById('id5').value=encodeURIComponent(str);
}
function decryptData(){
	var key=document.getElementById('id7').value;
	var iv=document.getElementById('id9').value;
	var str=document.getElementById('id2').value;
  console.log(typeof str);
	document.getElementById('id4').value=decrypt(key,iv,str);
}
function getRandomWord1(){
	document.getElementById('id7').value=randomWord(false,16,16);
}
function getRandomWord2(){
	document.getElementById('id9').value=randomWord(false,16,16);
}
/*
** randomWord 产生任意长度随机字母数字组合
** randomFlag-是否任意长度 min-任意长度最小位[固定位数] max-任意长度最大位
*/
function randomWord(randomFlag, min, max){
    var str = "",
        range = min,
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
 
    // 随机产生
    if(randomFlag){
        range = Math.round(Math.random() * (max-min)) + min;
    }
    for(var i=0; i<range; i++){
        pos = Math.round(Math.random() * (arr.length-1));
        str += arr[pos];
    }
    return str;
}
// 加密
function encrypt(key1,iv1,word){
    var key = CryptoJS.enc.Utf8.parse(key1);
    var iv1  = CryptoJS.enc.Utf8.parse(iv1);
    var srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv:iv1,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});
    return encrypted.toString();
}
//解密
function decrypt(key1,iv1,word){
    var key = CryptoJS.enc.Utf8.parse(key1);
    var iv1  = CryptoJS.enc.Utf8.parse(iv1);
    var decrypt = CryptoJS.AES.decrypt(word, key, {iv:iv1,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
</script>
</head>
<body>
<INPUT id="id0" onclick="" type="text" value="12345" name="test" style="width:600px">原始数据
<INPUT id="id1" onclick="encryptData()" type="button" value="加密" name="test" style="width:120px">
<br>
<INPUT id="id7" onclick="" type="text" value="0123456789abcdef" name="test" style="width:600px">密钥key
<INPUT id="id8" onclick="getRandomWord1()" type="button" value="生成随机密钥" name="test" style="width:120px">
<br>
<INPUT id="id9" onclick="" type="text" value="0123456789abcdef" name="test" style="width:600px">偏移量
<INPUT id="id10" onclick="getRandomWord2()" type="button" value="生成随机密钥" name="test" style="width:120px">
<br>
<INPUT id="id2" onclick="" type="text" value="" name="test" style="width:600px">加密结果
<INPUT id="id3" onclick="decryptData()" type="button" value="解密" name="test" style="width:120px">
<br>
<INPUT id="id4" onclick="" type="text" value="" name="test" style="width:600px">解密结果
<br>
<INPUT id="id5" onclick="" type="text" value="" name="test" style="width:600px">URI转码结果
<INPUT id="id6" onclick="encodeURL1()" type="button" value="URI转码" name="test" style="width:120px">

</body>
</html>
</html>

二、功能封装

1. 封装代码

// npm i crypto-js@4.1.1
// src\utils\crypto.js

// 加密解密
import CryptoJS from 'crypto-js'
// 加密
export const encryptionData = (word) => {
  // 这里要加.Base64。网上大多数例子都没有,否则和后端加密不同,解析不出来,坑的1b
  const key1 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('gisq39561c9fe068'))
  const key = CryptoJS.enc.Base64.parse(key1)
  const iv = key

  const srcs = CryptoJS.enc.Utf8.parse(word)
  // 加密模式cbc,补码Pkcs7,其实就是PKCS5Padding,都是和后端的加密模式对应的
  const encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })

  // 这里返回大小写随意,都能解析出来,统一点就用小写
  const hexStr = encrypted.ciphertext.toString().toLowerCase()

  return hexStr
}

// 解密
export const decryptData = (word) => {
  const key1 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse('gisq39561c9fe068'))
  const key = CryptoJS.enc.Base64.parse(key1)
  const iv = key
  const encrypted = CryptoJS.AES.decrypt(word, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
  return encrypted.toString(CryptoJS.enc.Utf8)
}

2. 使用说明

(1) 引入utils工具

import { encryptionData } from '@/utils/crypto'

(2) 使用工具

// 加密方法
const info = encryptionData(JSON.stringify({ username: this.username, password: this.password, subGuid: config.subGuid }))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值