错误-简单的加密和解密-Node.js

时间:2019-05-20 20:06:05

标签: javascript node.js encryption electron

我正在尝试开发一些非常简单的代码来加密和解密一些简单的文本。问题似乎是在运行代码时,我的createDecipherIV的.final()导致标题中出现该错误。

我尝试使用各种编码(二进制vs十六进制,base 64等)玩弄

节点:“ 10.15.3”

openssl:“ 1.1.0j”

这是一个电子项目,尽管我不知道可能会有什么影响

const crypto = require('crypto'); 
let sessionKey = crypto.randomBytes(256/8).toString('hex'); 

class encryption { 
    constructor() { 
        this.encryptionOptions = { 
            algorithm: 'aes-256-cbc',
            iv: crypto.randomBytes(16), 
            key: String, 
        }
    }
    encryptMem(memItem){
        this.encryptionOptions['key'] = Buffer.from(sessionKey,'hex'); 
        var cipher = crypto.createCipher(this.encryptionOptions['algorithm'], this.encryptionOptions['key'], this.encryptionOptions['iv']);
        var cipherText = cipher.update(memItem,'utf8','hex');
        cipherText += cipher.final('hex');
        return this.encryptionOptions['iv'].toString('hex') + cipherText;
    }
    decryptMem(memObject){
        this.encryptionOptions['key'] = Buffer.from(sessionKey,'hex'); 
        var _iv = Buffer.from(memObject.slice(0,32),'hex')
        var _data = memObject.slice(32)
        var _decode = crypto.createDecipheriv(this.encryptionOptions['algorithm'], this.encryptionOptions['key'], _iv); 
        var _decoded = _decode.update(_data,'hex','utf8'); 
        _decoded += _decode.final('utf8')
        return _decoded; 
    }
}

示例代码

e = new encryption 
encryption {encryptionOptions: {…}}
val = e.encryptMem("test") 
"adcd1f5876ca02a4420b61df5dfdaa9be3080108020df42dfc630951ffabe0ac"
e.decryptMem(val)
\lib\encrypt.js:25
internal/crypto/cipher.js:172 Uncaught Error: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
    at Decipheriv.final (internal/crypto/cipher.js:172)
    at encryption.decryptMem (\lib\encrypt.js:26)
    at <anonymous>:1:3
final @ internal/crypto/cipher.js:172
decryptMem @ \lib\encrypt.js:26
(anonymous) @ VM433:1

它应该只返回“ test

编辑正如答案中指出的那样,需要将createCipher更改为createCipheriv

以下是一些经过简化的代码,它们也应该在每次加密时正确地创建一个新的iv,而不是简单地通过类的调用

const crypto = require('crypto'); 
let sessionKey = crypto.randomBytes(256/8).toString('hex'); 

class encryption { 
    constructor(key = null){ 
        this.algorithm = 'aes-256-cbc';
        this.key = key || Buffer.from(sessionKey,'hex'); 
    }
    encrypt(plainText){
        var iv = crypto.randomBytes(16);  
        var cipher = crypto.createCipheriv(this.algorithm,this.key,iv);
        var cipherText = cipher.update(plainText,'utf8','hex') + cipher.final('hex');
        return iv.toString('hex') + cipherText;
    }
    decrypt(cipherText){
        var iv = Buffer.from(cipherText.slice(0,32),'hex');
        var data = cipherText.slice(32);
        var decode = crypto.createDecipheriv(this.algorithm,this.key,iv);
        var decoded = decode.update(data,'hex','utf8') + decode.final('utf8');
        return decoded; 
    }
}

1 个答案:

答案 0 :(得分:0)

crypto.createCipher

您的阻止模式(CBC)需要IV,但是您使用的是createCipher而不是createCipheriv。由于您正在使用createDecipheriv,因此我认为这是一个错字。