javascript - ScryptSync 密钥不能在 createCipheriv、Nodejs、加密库中使用
问题描述
我是与编码相关的主题的新手,我无法将我的 scryptsync 密钥转换为 createCipheriv(集成到 Nodejs 中的加密库)可以在 iv 参数中使用的东西。
const algorithm = 'aes-256-gcm';
var text = 'default'
var encrypted = secret;
class Auth
{
SignUp(pass)
{
console.log(pass);
var pair = ec.genKeyPair();
text = pair.getPrivate.toString('hex');
var key = crypto.scryptSync(pass, 'baethrowssalt', 32);
console.log(`The key is:${key}`); //this is not a string
key=key.toString('hex');
var cipher = crypto.createCipheriv(algorithm, key);
var encrypted = cipher.update(text, 'hex', 'hex') + cipher.final('hex');
fs.writeFileSync(file, encrypted);
return alert(`Close and reopen your app to integrate your wallet securely`);
}
正如您在上面看到的,我尝试将 scryptSync 返回转换为字符串十六进制,以便 createCipheriv 愿意使用它,但我收到以下错误:
pass is passrig
passrig
The key is:�→A�r;yR�����▲�h�8��f�����v�A�,
TypeError [ERR_INVALID_ARG_TYPE]: The "iv" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
at Cipheriv.createCipherWithIV (internal/crypto/cipher.js:120:29)
at new Cipheriv (internal/crypto/cipher.js:227:22)
at Object.createCipheriv (crypto.js:117:10)
所以我的问题包含两个部分:如何将 scryptSync 与 createCipheriv 一起使用?为了帮助我理解,您的解决方案背后的原因是什么?
解决方案
我创建了一个示例,该示例使用算法加密一些数据aes-256-gcm
并使用crypto.scryptSync从密码中派生密钥。
步骤如下:
key
使用 crypto.scryptSync 从我们的密码中派生出我们的密码。- 使用crypto.randomBytes创建一个 IV 。
- 使用我们的密钥和 iv 使用我们的 encrypt 函数加密明文。
- 通过使用我们的解密函数解密来测试我们的加密数据。
这段代码如下:
const crypto = require("crypto");
const Algorithm = "aes-256-gcm";
function encrypt(plainText, key, iv) {
const cipher = crypto.createCipheriv(Algorithm, key, iv);
return { encrypted: Buffer.concat([cipher.update(plainText), cipher.final()]), authTag: cipher.getAuthTag() }
}
function decrypt(encrypted, key, iv, authTag) {
const decipher = crypto.createDecipheriv(Algorithm, key, iv).setAuthTag(authTag);
return Buffer.concat([decipher.update(encrypted), decipher.final()]);
}
const password = "Speak Friend and Enter";
const plainText = "There is nothing either good or bad but thinking makes it so.";
const salt = crypto.randomBytes(32);
// Create an encryption key from our password, ensuring it is 32 bytes long - AES-256 needs a 256 bit (32 byte) key
const KEY = crypto.scryptSync(password, salt, 32);
const IV = crypto.randomBytes(16);
console.log("Key (derived from password, hex):", KEY.toString("hex"));
console.log("IV (hex):", IV.toString("hex"));
console.log("Plaintext:", plainText);
const { encrypted, authTag } = encrypt(plainText, KEY, IV);
console.log("Encrypted (hex):", encrypted.toString("hex"));
const decrypted = decrypt(encrypted, KEY, IV, authTag)
console.log("Decrypted:", decrypted.toString("utf-8"));
推荐阅读
- c++ - 迭代器和 STL 容器的关系
- python - 如何使用 pandas 循环创建变量并读取多个 excel 文件?
- python - 更改 mdf-object 中的特定值(python,asammdf)
- python - Python使用N个字典中具有相同条目但值不同的值列表制作主字典
- jboss-arquillian - Thorntail arquillian jacoco 测试用例覆盖率显示 0%
- tensorflow - 如何在 Tensorflow 2.0 + Keras 中进行并行 GPU 推理?
- makefile - 在 Makefile 中使用后缀规则
- python - aiopg + sqlalchemy:如何在没有原始 sql 的情况下“删除表(如果存在)”?
- python - 从 Python 脚本重定向 subprocess.run() 标准错误
- java - 如何在 Spring Boot 中使用外键 POST multipartFile?