首页 > 解决方案 > 为什么node的aes-256-gcm能产生长度不能被16整除的密文?

问题描述

尽管 AES-256-GCM 使用块大小为 16 字节的块 chiffre,但为什么以下 nodejs 代码会产生总长度不能被 16 整除的输出?

const crypto = require('crypto')
let iv = Buffer.from('ed655ce15162cecc9a0fc47fbc5055e2', 'hex')
let key = Buffer.from('17c0c95f4499dc8cb2b11d8c68569729484579df9adeceb7898fd9508915e46f', 'hex')
let cipher = crypto.createCipheriv('aes-256-gcm', key, iv)
let outputA = cipher.update("some string", 'utf8')
let outputB = cipher.final()

console.log(outputA.length, outputB.length) //output: "11 0"
//outputA = <Buffer 26 5b 11 63 cf 26 44 81 d2 1d 77>

标签: node.jscryptography

解决方案


显然 GCM 不需要填充,也不需要明文是块大小的倍数。

更多信息在这里


推荐阅读