为什么 AES 128 加密会扩展数据?

Why does AES 128 encryption expanding the data?

我使用下面的代码来加密数据。我的输入是 16 个字节,密钥是 16 个字节,但我得到的输出(加密数据)是 32 个字节。为什么?

 public static byte[] encrypt(byte[] plainText, byte[] key)  {
try {
  byte[] passwordKey128 = Arrays.copyOfRange(key, 0, 16);
  SecretKeySpec secretKey = new SecretKeySpec(passwordKey128, "AES");
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  byte[] cipherText = cipher.doFinal(plainText);
  // String encryptedString = Base64.getEncoder().encodeToString(cipherText);
  return cipherText;

可能是什么原因? AES 是否添加一些数据?

您通过 Cipher.getInstance(transformation) 方法获得 Cipher 对象,其中转换的形式为:

"algorithm/mode/padding" or
"algorithm"

执行此操作时,实施会搜索系统中的加密提供商列表,并确定是否有任何实施支持此操作。如果您不指定模式和填充,则由加密提供者决定使用什么默认模式和填充。

根据this,例如SunJCE默认ECB为默认模式,而PKCS5Padding.

由于 PKCS5Padding 总是至少添加一个字节,它会将您的 16 字节推到块的限制之上,从而需要两个块。