【问题标题】:Why is my decipher.update returning a function and not the deciphered text? NodeJS为什么我的 decipher.update 返回一个函数而不是解密的文本?节点JS
【发布时间】:2021-07-18 19:58:57
【问题描述】:

我正在使用内置的加密模块,并且沮丧了好几个小时试图弄清楚为什么 decipher.update 返回一个函数而不是解密的文本本身。

代码:

const file = path.join(__dirname, '../secret.txt');
const fileIV = path.join(__dirname, '../iv.txt');
const at = path.join(__dirname, '../at.txt')
var secret = fs.readFileSync(file, 'utf-8');

const algorithm = 'aes-256-gcm';
var text = 'default'
var encrypted = secret;
const iv = crypto.randomBytes(16);

encrypt(plainText, key, iv) {
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    return { encrypted: Buffer.concat([cipher.update(plainText), cipher.final()]), authTag: cipher.getAuthTag() }
}

decrypt(encrypted, key, iv, authTag) {
    const decipher = crypto.createDecipheriv(algorithm, key, iv).setAuthTag(authTag);
    console.log('this worked decrypt');
    return Buffer.concat([decipher.update(encrypted), decipher.final()]);
}

SignUp(pass)
{
    console.log(pass);

    var pair = ec.genKeyPair(); 

    text = pair.getPrivate.toString('hex');

    const key = crypto.scryptSync(pass, 'baethrowssalt', 32);

    console.log(`The key is:${key}`); 

    const {encrypted, authTag} = this.encrypt(text, key, iv);

    console.log('encrypted: ',encrypted.toString('hex'));

    const decrypted = this.decrypt(encrypted, key, iv, authTag);

    console.log('Decrypted:', decrypted.toString('utf-8'));

    return console.log(`Close and reopen your app to integrate your wallet securely`); 
}

当我打印出我最初尝试使用 scrypt 加密的私钥的解密结果时,它会在控制台中打印:

Decrypted: function getPrivate(enc) {
  if (enc === 'hex')
    return this.priv.toString(16, 2);
  else
    return this.priv;
}

为什么

decrypt(encrypted, key, iv, authTag) {
    const decipher = crypto.createDecipheriv(algorithm, key, iv).setAuthTag(authTag);
    console.log('this worked decrypt');
    return Buffer.concat([decipher.update(encrypted), decipher.final()]);
}

不给我解密形式的文本?另外,我怎么能得到它,因为我显然做错了什么。任何帮助真的将不胜感激。

【问题讨论】:

    标签: javascript node.js encryption aes-gcm scrypt


    【解决方案1】:

    这可能是因为“decrypted.toString('utf-8')”没有执行该函数,而是将其转换为字符串以显示在控制台中... 我相信您必须执行以下操作:

    let decryptedResult = decrypted.toString('utf-8'); console.log('Decrypted:', decryptedResult.toString('utf-8'));

    或但不确定

    console.log('Decrypted:', (decrypted).toString('utf-8'));

    【讨论】:

    • 感谢您抽出宝贵时间回答。这个很棘手。
    【解决方案2】:

    解密的结果和你加密的明文一模一样

    您可以通过在加密之前在控制台中的SignUp() 中输出明文(即text 的内容)来轻松验证这一点:

    var text = pair.getPrivate.toString('hex');             
    console.log('Initial plaintext:', text);    // Initial plaintext: function getPrivate(enc) {...
    

    text的内容出乎意料的原因是你根本忘了getPrivate后面的那对括号,应该是:

    var text = pair.getPrivate().toString('hex'); 
    console.log('Initial plaintext:', text);    // Initial plaintext: <the hex encoded private key>
    

    然后解密提供了预期的结果。

    【讨论】:

    • ?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️ ?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️?‍♂️在堆栈溢出时对你自己毫无意义的错误做出反应哈哈。谢谢你的回答,你不知道这对我有多大帮助。
    猜你喜欢
    • 2023-03-21
    • 2011-04-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多