¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | }; |
| | | |
| | | /** |
| | | * 使ç¨å¯é¥å¯¹æ°æ®è¿è¡å å¯ |
| | | * @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(); |
| | | }; |