Skip to content

Instantly share code, notes, and snippets.

@Houserqu
Last active March 24, 2022 09:00
Show Gist options
  • Save Houserqu/86a781d65f729d5dfe1e86016766a0cc to your computer and use it in GitHub Desktop.
Save Houserqu/86a781d65f729d5dfe1e86016766a0cc to your computer and use it in GitHub Desktop.
js加密
// 不同的算法要求的 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