当前位置:网站首页>crypto-js uses
crypto-js uses
2022-08-01 05:50:00 【no water in the sea】
crypto-js使用 :
const cryptoJs = require('crypto-js')
function getKey(key = "1234123412341234") {
return cryptoJs
.SHA1(cryptoJs.SHA1(key))
.toString()
.substring(0, 32);
}
function aesEncrypt(data, key) {
console.log('加密之前的data:', data+' key:'+ key + ' 真实的key:'+ getKey(key));
const KEY = cryptoJs.enc.Hex.parse(getKey(key));
const IV = cryptoJs.enc.Hex.parse(getKey(key));
if (typeof data === "object") {
try {
data = JSON.stringify(data);
} catch (error) {
console.log("加密失败:", error);
}
}
let encrypt = cryptoJs.AES.encrypt(
data,
KEY,
{
iv: IV,
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
);
console.log("加密之后的数据:", encrypt.ciphertext.toString());
return encrypt.ciphertext.toString();
}
function aesDecrypt(data, key) {
console.log('解密之前的data:', data+' key:'+ key + ' 真实的key:'+ getKey(key));
const KEY = cryptoJs.enc.Hex.parse(getKey(key));
const IV = cryptoJs.enc.Hex.parse(getKey(key));
let str = cryptoJs.enc.Base64.stringify(cryptoJs.enc.Hex.parse(data));
let decrypt = cryptoJs.AES.decrypt(
str,
KEY,
{
iv: IV,
mode: cryptoJs.mode.ECB,
padding: cryptoJs.pad.Pkcs7
}
);
console.log('解密后的数据:', decrypt.toString(cryptoJs.enc.Utf8).toString())
return decrypt.toString(cryptoJs.enc.Utf8);
}
let obj = {
name: '狠人大帝'
}
let a = aesEncrypt(obj, '45674567456745674567')
console.log('加密后的a====', a);
let a1 = aesDecrypt(a, '45674567456745674567')
console.log('解密后的a1', a1)
// export { aesEncrypt, aesDecrypt };
边栏推荐
- Robot_Framework: keyword
- 【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
- Talk about the bugs in using for in to traverse the array in js
- The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
- A,H,K,N
- 【视觉SLAM十四讲】第一章理论详解
- 深度比较两个对象是否相同
- 小心你的字典和样板代码
- Jupyter shortcuts
- 微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
猜你喜欢
first unique character in characters
Check控件
Selenium:操作Cookie
【MySQL必知必会】 表的优化 | 充分利用系统资源
Robot_Framework:关键字
Flip letters using string container
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
Windows taskbar icon abnormal solution
JS的运行原理
随机推荐
Motion analysis and parameter optimization of crank-slider mechanism
史上超强最常用SQL语句大全
Check控件
LeetCode 0149. 直线上最多的点数
LeetCode每日一题(309. Best Time to Buy and Sell Stock with Cooldown)
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
Dell PowerEdge Server R450 RAID Configuration Steps
响应式织梦模板园林景观类网站
CSP-S2019 Day1
Flip letters using string container
2022.7.26 Mock Competition
Robot_Framework: Assertion
点餐系统数据库设计--SQL Server
基于MATLAB的BP神经网络进行语音特征信号分类
【视觉SLAM十四讲】第一章理论详解
Leetcode第 304 场周赛
matplotlib pyplot
第6章——数据库的安全性
Selenium: Popup Handling
NDK does not contain any platforms problem solving