Node.js NodeJS解密des3unicode

Node.js NodeJS解密des3unicode,node.js,encryption,unicode,Node.js,Encryption,Unicode,我有以下代码片段 var crypto = require("crypto"); var iv = new Buffer('d146ec4ce3f955cb', "hex"); var key = new Buffer('dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4', "hex"); var encrypted = new Buffer('6A57201D19B07ABFAE74B453BA46381C', "hex"); var ciph

我有以下代码片段

var crypto = require("crypto");
var iv = new Buffer('d146ec4ce3f955cb', "hex");
var key = new Buffer('dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4', "hex");
var encrypted = new Buffer('6A57201D19B07ABFAE74B453BA46381C', "hex");

var cipher = crypto.createDecipheriv('des3', key, iv);
var result = cipher.update(encrypted);
result += cipher.final();

console.log("result: " + result);
结果是“密码” 此代码段对于基于ASCII的单词非常有用。 但是,我有一些unicode密码

例如,这是π:

UU__3185CDAA15C1CDED
我尝试使用这个值,加上删除“UU_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。 对于加密数据,我也尝试了类似的方法:

var encrypted = new Buffer('UU__3185CDAA15C1CDED', "utf16le");

但是不行。。 我得到以下错误

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decr   ypt
    at Error (native)
    at Decipheriv.Cipher.final (crypto.js:202:26)
    at Object.<anonymous> (/Users/miker/Local Projects/rec10_decryption/server2.js:14:18)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
错误:错误:06065064:数字信封例程:EVP_decrypt final_ex:错误解密
错误(本机)
在Decipheriv.Cipher.final(crypto.js:202:26)中
反对。(/Users/miker/localprojects/rec10_decryption/server2.js:14:18)
在模块处编译(Module.js:460:26)
在Object.Module.\u extensions..js(Module.js:478:10)
在Module.load(Module.js:355:32)
在Function.Module.\u加载(Module.js:310:12)
位于Function.Module.runMain(Module.js:501:10)
启动时(node.js:129:16)
在node.js:814:3

任何帮助都将不胜感激。

删除
UU\UU
前缀并使用此代码对我有效:

var crypto = require('crypto');
var iv = new Buffer('d146ec4ce3f955cb', 'hex');
var key = new Buffer('dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4', 'hex');
var encrypted = new Buffer('3185CDAA15C1CDED', 'hex');

var cipher = crypto.createDecipheriv('des3', key, iv);
var result = Buffer.concat([
  cipher.update(encrypted),
  cipher.final()
]).toString('ucs2');

console.log('result: ' + result);
// outputs: result: Π

执行
result+=cipher.final()
时,首先将
result
从缓冲区转换为(utf8)字符串,然后追加
cipher.final()
从缓冲区转换为(utf8)字符串。当您使用多字节字符时,如果在对
.update()
.final()
的调用中使用字符的字节,则可能会导致数据损坏。将它们保留为缓冲区,将它们连接为二进制,然后将最终连接的结果转换为utf16字符串将起作用,而且更安全。

这非常完美。谢谢你的解释和清晰。
var crypto = require('crypto');
var iv = new Buffer('d146ec4ce3f955cb', 'hex');
var key = new Buffer('dc5c3319dc25c1f6f11f6a792a6dd28864c9dd48be26c2e4', 'hex');
var encrypted = new Buffer('3185CDAA15C1CDED', 'hex');

var cipher = crypto.createDecipheriv('des3', key, iv);
var result = Buffer.concat([
  cipher.update(encrypted),
  cipher.final()
]).toString('ucs2');

console.log('result: ' + result);
// outputs: result: Π