js 加密 crypto.js
Posted zhanglw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 加密 crypto.js相关的知识,希望对你有一定的参考价值。
1
import CryptoJS from ‘crypto-js‘
export const encrypt = (word) => {
var key = CryptoJS.enc.Utf8.parse("46cc793c53dc451b");
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
/**
* 解密
*/
export const decrypt = (word) => {
var key = CryptoJS.enc.Utf8.parse("46cc793c53dc451b");
var decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
2
const CryptoJS = require(‘crypto-js‘); //引用AES源码js
const key = CryptoJS.enc.Utf8.parse("1234123412ABCDEF"); //十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse(‘ABCDEF1234123412‘); //十六位十六进制数作为密钥偏移量
//解密方法
function Decrypt(word) {
let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
//加密方法
function Encrypt(word) {
let srcs = CryptoJS.enc.Utf8.parse(word);
let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
export default {
Decrypt ,
Encrypt
}
https://www.jianshu.com/p/95d8eeb8301f
https://www.jianshu.com/p/a47477e8126a
https://blog.csdn.net/vieri_32/article/details/48345023
https://blog.csdn.net/qq_28205153/article/details/55798628
以上是关于js 加密 crypto.js的主要内容,如果未能解决你的问题,请参考以下文章
C# 中用DES 对称Key,IV 加密,前端crypto.js 解密