当前位置:网站首页>crypto-js使用
crypto-js使用
2022-08-01 05:28:00 【大海里没有水】
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 };

边栏推荐
猜你喜欢

Malicious attacks on mobile applications surge by 500%

JS的运行原理

I met a shell script

可持久化线段树

Power button (LeetCode) 212. The word search II (2022.07.31)

2022.7.26 Mock Competition

USB3.0:VL817Q7-C0的LAYOUT指南(二)

Robot_Framework: keyword

关于给Qt做一个软件初始化的进度条
![[target detection] YOLOv7 theoretical introduction + practical test](/img/ff/a83acbf9dd5cc2f907f3538d287842.png)
[target detection] YOLOv7 theoretical introduction + practical test
随机推荐
ORACLE modify another user package (package)
Qt Widget 项目对qml的加载实例
Power button (LeetCode) 212. The word search II (2022.07.31)
[target detection] YOLOv7 theoretical introduction + practical test
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
微信小程序获取手机号phonenumber.getPhoneNumber接口开发
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
说说js中使用for in遍历数组存在的bug
AspNet.WebApi.Owin custom Token request parameters
4D line-by-line analysis and implementation of Transformer, and German translation into English (3)
Selenium: Dropdown Box Actions
uva10825
关于给Qt做一个软件初始化的进度条
字符中的第一个唯一字符
解决浏览器滚动条导致的页面闪烁问题
ApiFile
Robot growth in China
2022.7.26 Mock Competition
leetcode43 string multiplication
Selenium:元素等待