gpt4 book ai didi

node.js - 迁移 createCipher 和 createCipheriv

转载 作者:行者123 更新时间:2023-12-02 07:58:37 25 4
gpt4 key购买 nike

我刚刚移至 Node 12.13,并且 crypto.createDeciphercrypto.createCipher 遇到一些问题

首先,当我使用这两个函数时,我收到了弃用警告。

const encodeString = (value, password) =>
new Promise((resolve, reject) => {
const cipher = crypto.createCipher("aes192", password);
let encrypted = "";
cipher.on("readable", () => {
const data = cipher.read();
if (data) encrypted += data.toString("hex");
});
cipher.on("end", () => resolve(encrypted));

cipher.write(value);
cipher.end();
});

const decodeString = (encrypted, password) =>
new Promise((resolve, reject) => {
const decipher = crypto.createDecipher("aes192", password);
let decrypted = "";
decipher.on("readable", () => {
const data = decipher.read();
if (data) decrypted += data.toString("utf8");
});
decipher.on("end", () => resolve(decrypted));

decipher.write(encrypted, "hex");
decipher.end();
});

data()
.then(data => {
console.log("final", data);
})
.catch(err => {
console.log("final err", err);
});

我正在寻找一种迁移到 createCipherivcreateDecipheriv 的方法,但我找不到如何将密码转换为 key 和 iv

最佳答案

找到以下方法来生成 key 和 IV 以及实现方法

const keyBytes: Buffer = Buffer.from(password, 'base64');
// Generates 16 byte cryptographically strong pseudo-random data as IV
// https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
const ivBytes: Buffer = crypto.randomBytes(16);
const ivText: string = ivBytes.toString('base64');

加密

export function encryptString(plainText: string, secret: string): string {
if (!plainText || plainText.length === 0) {
return plainText;
}
if (!secret || secret.length === 0) {
throw new Error('you must pass a secret');
}
const keyBytes: Buffer = Buffer.from(secret, 'base64');
// Generates 16 byte cryptographically strong pseudo-random data as IV
// https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
const ivBytes: Buffer = crypto.randomBytes(16);
const ivText: string = ivBytes.toString('base64');
// encrypt using aes256 iv + key + plainText = encryptedText
const cipher: crypto.Cipher = crypto.createCipheriv('aes-256-cbc', keyBytes, ivBytes);
let encryptedValue: string = cipher.update(plainText, 'utf8', 'base64');
encryptedValue += cipher.final('base64');
// store base64(ivBytes)!base64(encryptedValue)
return `${ ivText }!${ encryptedValue }`;
}

解密

    export function decryptString(encryptedValue: string, secret: string):string {

if (!encryptedValue || encryptedValue.length === 0) {
return encryptedValue;
}

if (!secret || secret.length === 0) {
throw new Error('you must pass a secret');
}

const parts: string[] = encryptedValue.split('!');
if (parts.length !== 2) {
throw new Error('The encrypted value is not a valid format');
}
const ivText: string = parts[0];
const encryptedText: string = parts[1];
const ivBytes: Buffer = Buffer.from(ivText, 'base64');
const keyBytes: Buffer = Buffer.from(secret, 'base64');

if (ivBytes.length !== 16) {
throw new Error('The encrypted value is not a valid format');
}

if (keyBytes.length !== 32) {
throw new Error('The secret is not valid format');
}

// decrypt using aes256 iv + key + encryptedText = decryptedText
const decipher: crypto.Decipher = crypto.createDecipheriv('aes-256-cbc', keyBytes, ivBytes);
let value: string = decipher.update(encryptedText, 'base64', 'utf8');
value += decipher.final('utf8');
return value;}

关于node.js - 迁移 createCipher 和 createCipheriv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com