当前位置:网站首页>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 };

边栏推荐
猜你喜欢
随机推荐
中国的机器人增长
first unique character in characters
Vsce package after the Command failed: NPM list - production - parseable - the depth = 99999 - loglevel = error exception
LeetCode 0149. Maximum number of points on a line
滚动条样式修改
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
WPF项目-按着键盘方向键,移动格子盒子效果
Does flinkcdc have any solution for mysql's date field type conversion?
Selenium: mouse, keyboard events
测试工具(四)Jenkins环境搭建与使用
Seleniu:元素常用操作
crypto-js使用
MySQL-DML language-database operation language-insert-update-delete-truncate
安装SQL Server详细教程
响应式织梦模板园林景观类网站
NUMPY
The solution to the inconsistency between the PaddleX deployment inference model and the GUI interface test results
第5章——以程序方式处理MySQL数据表的数据
将CSV文件快速导入MySQL中
Compare two objects are the same depth









