Last active
March 24, 2022 09:00
-
-
Save Houserqu/86a781d65f729d5dfe1e86016766a0cc to your computer and use it in GitHub Desktop.
js加密
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 不同的算法要求的 key 长度不一样 | |
// 算法 key iv | |
// 128 16 16 | |
// 192 24 16 | |
// 256 32 16 | |
// 示例 key d42308a3a6f9db4e6eeff373e1b34663 iv 8109f3a93716beab | |
/** | |
* aes 加密封装 | |
* @param data | |
* @param key | |
* @returns | |
*/ | |
export function aesEncrypt(data, key, iv) { | |
const cipher = crypto.createCipheriv('aes256', Buffer.from(key), iv); | |
return cipher.update(data, 'binary', 'base64') + cipher.final('base64'); | |
} | |
/** | |
* aes 解密封装 | |
* @param encrypted | |
* @param key | |
* @returns | |
*/ | |
export function aesDecrypt(encrypted, key, iv) { | |
const decipher = crypto.createDecipheriv('aes256', key, iv); | |
return decipher.update(encrypted, 'base64', 'utf8') + decipher.final('utf8'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment