我正在传递 20 字节长的输入数据,java AES-CBC 返回 48 字节而不是 32 字节,这是我认为由于填充而应该输出的。我的密钥长 16 个字节。
byte[] ciphertext;
byte[] thekey = new byte[16];
new Random().nextBytes(thekey);
byte[] vector = new byte[16];
new Random().nextBytes(vector);
String s = "c6be25d903159d680d81f3d99bb702451e9f7158";
byte[] data = s.getBytes();
Cipher enc = Cipher.getInstance("AES/CBC/PKCS5Padding");
enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(thekey, "AES"),
new IvParameterSpec(vector));
ciphertext = enc.doFinal(data);
/* Sample Output*/
StringBuffer testvec = new StringBuffer();
StringBuffer test = new StringBuffer();
StringBuffer testkey = new StringBuffer();
for (byte b:vector){
testvec.append(String.format("%02x", b));
}
System.out.println("Vector:" + " " +testvec.toString());
for (byte b:ciphertext){
test.append(String.format("%02x", b));
}
System.out.println(" Cipher:"+ " " + test.toString());
for (byte b:thekey){
testkey.append(String.format("%02x", b));
}
System.out.println("theKey:"+ " " + testkey.toString());
样本输出:
向量:c6ab4c2b0b220b8b3520bd20e3741a1e
密码:3dd2cb1f94c99940fd4f7d1a503a091844dc16c8bae480d748453859701b72fecd949e158d2103ba99560d64ee65f6cb
密钥:bc03f2e674a0d482d0c6677d211eb14e