Reputation: 1480
I am still rather new to all the terminology around cryptology, so please excuse my ignorance on the subject. I am having something strange happening when using node.js' crypto module. It will encrypt exactly 16 characters only. Any more and it fails with this error message:
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)
The code I am using looks like this:
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');
}
Why does this not encrypt more than 16 characters? Is it because of the algorithm I am using? How can I encrypt something without caring how long it is?
Upvotes: 1
Views: 1110
Reputation: 106696
The problem is that you're discarding part of your encrypted and decrypted data. update()
can return data just like final()
can. So change your encrypt()
and decrypt()
like:
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');
}
Upvotes: 1