1

私はまだ暗号に関するすべての用語にかなり慣れていないので、この件に関する私の無知を許してください。node.js の crypto モジュールを使用しているときに、奇妙なことが起こっています。正確に 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');
}

これが 16 文字以上を暗号化しないのはなぜですか? 使用しているアルゴリズムのせいですか?長さを気にせずに何かを暗号化するにはどうすればよいですか?

4

1 に答える 1

1

問題は、暗号化および復号化されたデータの一部を破棄していることです。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');
}
于 2014-10-26T13:56:46.390 に答える