前端des加密信息
【摘要】
idnumberDes.js 加解密封装 需要先安装 CryptoJS npm i crypto-js
import CryptoJS from "crypto-js";
// des加密,isHex...
idnumberDes.js
加解密封装
需要先安装 CryptoJS
npm i crypto-js
import CryptoJS from "crypto-js";
// des加密,isHex表示结果是否有密文,输出hex,否则输出base64
// key为加密的秘钥。message 为需要加密的信息
let encryptByDES = function (message, key, isHex = true) {
var keyHex = CryptoJS.enc.Utf8.parse(key);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
if (isHex) return encrypted.ciphertext.toString();
else return encrypted.toString();
}
// DES 解密,isHex表示对hex解密,否则对base64解密
let decryptByDES = (ciphertext, key, isHex = true) => {
var keyHex = CryptoJS.enc.Utf8.parse(key);
if (isHex) ciphertext = CryptoJS.enc.Hex.parse(ciphertext)
else ciphertext = CryptoJS.enc.Base64.parse(ciphertext)
var decrypted = CryptoJS.DES.decrypt({ ciphertext }, keyHex, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return decrypted.toString(CryptoJS.enc.Utf8);
}
export {
encryptByDES, decryptByDES
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
用法:
import { encryptByDES } from '../../../utils/idnumberDes';
- 1
// message 加密信息 key加密秘钥 key与后端定义一致
let num = encryptByDES(message,key)
console.log(num)
- 1
- 2
- 3
文章来源: jiangwenxin.blog.csdn.net,作者:前端江太公,版权归原作者所有,如需转载,请联系作者。
原文链接:jiangwenxin.blog.csdn.net/article/details/122122861
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)