为什么节点AES128密码的输出长度为32字节?

这是我的代码:

var crypto = require('crypto') function aese (key) { return crypto.createCipher('aes128', key) } var x1 = crypto.randomBytes(16) var y1 = crypto.randomBytes(16) var a = aese(y1) a.write(x1) a.end() var ct = a.read() console.log(ct.length); //should be 16 but is 32 

这是由于填充增加了至less一个字节,16个字节中至less有17个输出,由于块大小,填充到32个字节。

尝试减lessencryption字节数为15,你会得到16个字节作为输出。

另一种select是closures填充 ;

 var x1 = crypto.randomBytes(16) var y1 = crypto.randomBytes(16) var a = aese(y1) a.setAutoPadding(false); <--- turn of auto-padding a.write(x1) a.end() var ct = a.read() console.log(ct.length); <--- gives 16 as output