一、基本使用
1. 资源下载
crypto-js
2. 目录结构
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d9d9707c1696a2e03def24bc651784cf.png)
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);
}
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. 封装代码
import CryptoJS from 'crypto-js'
export const encryptionData = (word) => {
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)
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 }))