对于 aes256,您可以执行以下操作:
const crypto = require("crypto");
const initVector = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
]);
function encryptString(string, securityKey) {
const cipher = crypto.createCipheriv(
'aes-256-cbc',
securityKey,
initVector
);
cipher.update(string, 'utf-8', 'hex');
return cipher.final('hex');
}
function decryptString(string, securityKey) {
const decipher = crypto.createDecipheriv(
'aes-256-cbc',
securityKey,
initVector
);
decipher.update(string, 'hex', 'utf-8');
return decipher.final('utf-8');
}
这initVector
是提供算法的初始状态,您可以将其更改为您想要的任何内容,但它应该是一个正好 16 个字节的数组,然后只需使用这些函数:
const securityKey = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32
]);
const text = 'example string';
const encrypted = encryptString(text, securityKey);
const decrypted = decryptString(encrypted, securityKey);
console.log(encrypted);
console.log(decrypted);
securityKey
密钥是将用于加密和解密字符串的密码,它应该是一个正好 32 字节的数组,将其更改为您想要的任何内容!