123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import CryptoJS from 'crypto-js';
- /**
- * 随机生成32位的字符串
- * @returns {string}
- */
- const generateRandomString = () => {
- const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- let result = '';
- const charactersLength = characters.length;
- for (let i = 0; i < 32; i++) {
- result += characters.charAt(Math.floor(Math.random() * charactersLength));
- }
- return result;
- };
- /**
- * 随机生成aes 密钥
- * @returns {string}
- */
- export const generateAesKey = () => {
- return CryptoJS.enc.Utf8.parse(generateRandomString());
- };
- /**
- * 加密base64
- * @returns {string}
- */
- export const encryptBase64 = (str: CryptoJS.lib.WordArray) => {
- return CryptoJS.enc.Base64.stringify(str);
- };
- /**
- * 解密base64
- */
- export const decryptBase64 = (str: string) => {
- return CryptoJS.enc.Base64.parse(str);
- };
- /**
- * 使用密钥对数据进行加密
- * @param message
- * @param aesKey
- * @returns {string}
- */
- export const encryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => {
- const encrypted = CryptoJS.AES.encrypt(message, aesKey, {
- mode: CryptoJS.mode.ECB,
- padding: CryptoJS.pad.Pkcs7
- });
- return encrypted.toString();
- };
- /**
- * 使用密钥对数据进行解密
- * @param message
- * @param aesKey
- * @returns {string}
- */
- export const decryptWithAes = (message: string, aesKey: CryptoJS.lib.WordArray) => {
- const decrypted = CryptoJS.AES.decrypt(message, aesKey, {
- mode: CryptoJS.mode.ECB,
- padding: CryptoJS.pad.Pkcs7
- });
- return decrypted.toString(CryptoJS.enc.Utf8);
- };
|