我使用以下代码进行 AES-128 加密来编码一个 16 字节的块,但编码值的长度给出了 2 个 32 字节的块。我错过了什么吗?
plainEnc = AES.encrypt("thisisapassword!");
导入java.security.*;
导入 java.security.spec.InvalidKeySpecException;
导入 javax.crypto.*;
导入 sun.misc.*;
公共类 AES {
私有静态最终字符串 ALGO = "AES";
私有静态最终字节[] keyValue =
新字节[] {'T','h','e','B','e','s','t',
'密钥' };
公共静态字符串加密(字符串数据)抛出异常{
System.out.println("字符串长度:" + (Data.getBytes()).length); //长度 = 16
键键 = generateKey();
密码芯片 = Cipher.getInstance(ALGO);
Chiper.init(Cipher.ENCRYPT_MODE,键);
byte[] encVal = chiper.doFinal(Data.getBytes());
System.out.println("输出长度:" + encVal.length); //长度 = 32
String encryptedValue = new BASE64Encoder().encode(encVal);
返回加密值;
}
公共静态字符串解密(字符串加密数据)抛出异常 {
键键 = generateKey();
密码芯片 = Cipher.getInstance(ALGO);
Chiper.init(Cipher.DECRYPT_MODE,键);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
字节[] decValue = chiper.doFinal(decordedValue);
字符串解密值 = 新字符串(decValue);
返回解密值;
}
私有静态密钥 generateKey() 抛出异常 {
Key key = new SecretKeySpec(keyValue, ALGO);
返回键;
}
}