首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NodeJs createDecipheriv解密密文。我做错了什么?

使用NodeJs createDecipheriv解密密文。我做错了什么?
EN

Stack Overflow用户
提问于 2013-07-31 23:41:44
回答 2查看 11.4K关注 0票数 5

我有以下代码可以解密加密的文本:

代码语言:javascript
复制
var crypto = require('crypto');
var Buffer = require('buffer').Buffer;

var iv = new Buffer('the-iv', 'binary'); //length=16
var key = new Buffer('the-secret-key', 'binary');//length=30

var encryptedText = new Buffer('base64-encoded-encrypted-data', 'base64');

var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); //using the aes-128-cbc algorithm
decrypted = decipher.update(encryptedText, "binary", 'utf8');
decrypted += decipher.final('utf8');

当我执行脚本node test-main.js时,我得到以下错误:

代码语言:javascript
复制
node-crypto : Invalid key length 30

crypto.js:355
  this._binding.initiv(cipher, toBuf(key), toBuf(iv));
                ^
Error: DecipherInitIv error
    at new Decipheriv (crypto.js:355:17)
    at Object.Decipheriv (crypto.js:352:12)
    at Object.<anonymous> (path/to/file/test-main.js:9:19)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

我的NodeJs版本是0.10.15

我不确定我做错了什么/遗漏了什么。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-01 00:23:58

请尝试使用长度为16的密钥。aes- 128 -cbc使用128位密钥。

票数 15
EN

Stack Overflow用户

发布于 2016-03-15 21:14:23

代码语言:javascript
复制
    var crypto = require('crypto');// dont forget to include this line i am just unable to put this in the code
    var data = JSON.stringify({someKey: "someValue"});

    console.log('Original cleartext: ' + data);
    var algorithm = 'aes-128-ecb';
    var key = 'myVeryTopSecretK';
    var clearEncoding = 'buffer';
    var cipherEncoding = 'base64';

    var cipher = crypto.createCipheriv(algorithm, key, new Buffer(""));

    var cipherChunks = [];
    cipherChunks.push(cipher.update(new Buffer(JSON.stringify({someKey: "someValue"}), 'utf8'), clearEncoding, cipherEncoding));
    cipherChunks.push(cipher.final(cipherEncoding));
    console.log(cipherEncoding + ' ciphertext: ' + cipherChunks.join(''));
    var decipher = crypto.createDecipheriv(algorithm, key, new Buffer(""));
    var plainChunks = [];
    for (var i = 0;i < cipherChunks.length;i++) {
      plainChunks.push(decipher.update(cipherChunks[i], cipherEncoding, clearEncoding));

    }
    plainChunks.push(decipher.final(clearEncoding));
    console.log("UTF8 plaintext deciphered: " + plainChunks.join(''));
//// dont forget to include this line i am just unable to put this in the code
var crypto = require('crypto');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17974846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档