javascript使⽤crypto-js进⾏AES加密解密算法(CBC模式)
// 引⼊crypto-js包
import CryptoJS from 'crypto-js'
/**
* AES加密
* @param {any} word 加密数据
*/
export const cryptoEncrypt = (word) => {
var key = Utf8.parse('填写密钥');
var iv = Utf8.parse('填写偏移量');
var encrypted = '';
if (typeof(word) == 'string') {
var srcs = Utf8.parse(word);
encrypted = pt(srcs, key, {
iv: iv,
mode: de.CBC,
padding: CryptoJS.pad.Pkcs7
});
} else if (typeof(word) == 'object') {//对象格式的转成json字符串
data = JSON.stringify(word);
var srcs = Utf8.parse(data);
encrypted = pt(srcs, key, {
iv: iv,
mode: de.CBC,
padding: CryptoJS.pad.Pkcs7
})
}
return String();
}
/**
* AES解密
* @param {any} word 解密数据
*/
如何下载javascript
export const cryptoDecrypt = (word) => {
var key = Utf8.parse('填写密钥');
var iv = Utf8.parse('填写偏移量');
var encryptedHexStr = Hex.parse(word);
var srcs = Base64.stringify(encryptedHexStr);
var decrypt = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: de.CBC,
padding: CryptoJS.pad.Pkcs7
});
var decryptedStr = Utf8);
String();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论