我传递的输入数据长度为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
theKey:bc03f2e674a0d482d0c6677d211eb14e
什么是数据? – 2014-11-09 14:18:51
关于Crypto.SE的相同问题:http://crypto.stackexchange.com/questions/20114/why-is-there-an-extra-block-when-encrypting – 2014-11-09 14:47:18
如果您不能复制和粘贴可编译的内容代码,那么我们无法回答你的问题。 String类没有getbytes方法。 – 2014-11-09 15:44:10