私は暗号化に非常に慣れていないと言って始めましょう。Node.js でCipher Block Chaining Modeを実装しようとしています。
私の問題は、復号化なしの暗号化の後、1 つの復号化関数呼び出しで機能しなくなることです。これが私のコードです:
var crypto = require('crypto');
var encryptionMethod = 'aes-256-cbc';
var vector = new Buffer([0xF1, 0x4C, 0xB6, 0xBD, 0x82, 0x93, 0x3C, 0x97, 0x6A, 0x4B, 0x4A, 0xD2, 0xAD, 0xD5, 0xA8, 0x6D]);
var key = new Buffer([59, 92, 128, 239, 136, 26, 19, 26, 226, 234, 53, 71, 157, 113, 209, 96, 111, 83, 167, 123, 217, 107, 124, 31, 238, 176, 58, 110, 161, 82, 81, 69]);
var cipher = crypto.createCipheriv(encryptionMethod, key, vector);
cipher.setAutoPadding(false);
var decipher = crypto.createDecipheriv(encryptionMethod, key, vector);
decipher.setAutoPadding(false);
var encrypt = function(array) {
return cipher.update(new Buffer(array));
};
var decrypt = function(buffer) {
return decipher.update(buffer);
};
var data = [];
for (var i = 0; i < 32; i++) {
data.push(i);
}
// no problem here (probably because the vector updates itself?)
console.log(decrypt(encrypt(data))); // <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f>
console.log(decrypt(encrypt(data))); // <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f>
console.log(decrypt(encrypt(data))); // <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f>
// after one encryption without a decryption it stops working.
console.log((encrypt(data)));
// why can't this be decrypted correctly? The last 16 entries are correct.
console.log(decrypt(encrypt(data))); // <Buffer e2 df 50 63 c7 eb 06 4c 28 19 6d 04 41 bd c0 db 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f>
// expected result
console.log(decrypt(encrypt(data))); // <Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f>
console.log
詳細については、コールの上のコメントを参照してください。decrypt
関数が常に機能していることを確認するにはどうすればよいですか?