NodeJS Decrypt des3 Unicode

我有以下的代码片段

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密码。

所以比如这个Pi:

 UU__3185CDAA15C1CDED 

我尝试过使用这个值,加上“UU__”的去除,但没有获得。 我也尝试了这样的encryption数据:

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

 var result = cipher.update(encrypted, 'ucs2'); 

但没有去..我得到以下错误

 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 

任何援助将不胜感激。

删除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 ,首先将Buffer的result转换为(utf8)string,然后将从Buffer转换的cipher.final()附加到(utf8)string中。 当您有多字节字符时,如果字符的字节跨越.update().final()调用,可能会导致数据损坏。 将它们保持为缓冲区,将它们连接为二进制, 然后将最终的连接结果转换为utf16string将会工作,并且更安全。