node.jsencryption模块不能encryption16个以上的字符

我对密码学的所有术语还比较陌生,所以请原谅我对这个问题的无知。 使用node.js的encryption模块时,发生了一些奇怪的事情。 它只会encryption正好16个字符。 任何更多,它失败,这个错误消息:

TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt at Decipher.Cipher.final (crypto.js:292:27) at decrypt (C:\node_apps\crypto_test\app.js:39:21) at C:\node_apps\crypto_test\app.js:16:21 at Interface._onLine (readline.js:200:5) at Interface._line (readline.js:531:8) at Interface._ttyWrite (readline.js:760:14) at ReadStream.onkeypress (readline.js:99:10) at ReadStream.emit (events.js:98:17) at emitKey (readline.js:1095:12) at ReadStream.onData (readline.js:840:14) 

我使用的代码如下所示:

 var rl = require('readline'); var crypto =require('crypto'); var interface = rl.createInterface({ input: process.stdin, output:process.stdout }); interface.question('Enter text to encrypt: ',function(texto){ var encrypted = encrypt(texto); console.log('Encrypted text:',encrypted); console.log('Decrypting text...'); var decrypted = decrypt(encrypted); console.log('Decrypted text:',decrypted); process.exit(); }); function encrypt(text) { var cipher =crypto.createCipher('aes192','password'); text = text.toString('utf8'); cipher.update(text); return cipher.final('binary'); } function decrypt(text) { var decipher = crypto.createDecipher('aes192','password'); decipher.update(text,'binary','utf8'); return decipher.final('utf8'); } 

为什么这不会encryption超过16个字符? 是因为我使用的algorithm吗? 我怎么能encryption一些东西而不关心它有多长?

问题是,你放弃了你的encryption和解密数据的一部分。 update()可以像final()一样返回数据。 所以改变你的encrypt()decrypt()如:

 function encrypt(text) { var cipher =crypto.createCipher('aes192', 'password'); text = text.toString('utf8'); return cipher.update(text, 'utf8', 'binary') + cipher.final('binary'); } function decrypt(text) { var decipher = crypto.createDecipher('aes192', 'password'); return decipher.update(text, 'binary', 'utf8') + decipher.final('utf8'); }