* AES 加密 * @param {string} data - * @param {string} baseKey - * @param {string} ivStr - * @returns {string} -
(data, baseKey = KEY_AES, ivStr = IV)
| 47 | * @returns {string} - |
| 48 | */ |
| 49 | function AESencrypt(data, baseKey = KEY_AES, ivStr = IV) { |
| 50 | let key = Buffer.from(baseKey, "utf8") |
| 51 | let iv = Buffer.from(ivStr, "utf8") |
| 52 | |
| 53 | // 填充长度 |
| 54 | if (key.length < 32) { |
| 55 | const paddedKey = Buffer.alloc(32); |
| 56 | key.copy(paddedKey); |
| 57 | key = paddedKey; |
| 58 | } |
| 59 | if (iv.length < 16) { |
| 60 | const paddedIV = Buffer.alloc(16); |
| 61 | iv.copy(paddedIV); |
| 62 | iv = paddedIV; |
| 63 | } const cipher = crypto.createCipheriv("aes-256-cbc", key, iv) |
| 64 | const dest = cipher.update(data, "utf8", "base64") + cipher.final("base64") |
| 65 | return dest.toString() |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * AES 解密 |