AES每次加密的block长度为16字节,密码也是16个字节。JAVA中该算法名为 AES
加密结果:
try
{
String password = "0123456789abcdef";
byte[] b = password.getBytes();
SecretKeySpec sks = new SecretKeySpec(b, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, sks);
String plain = "shaofa0012345678";
byte [] input = plain.getBytes();
byte [] output = c.update(plain.getBytes());
System.out.println("haha");
}
catch(Exception e)
{
e.printStackTrace();
}
加密结果:
-6 51 117 126 30 53 7 76 120 -89 91 -99 -94 -59 52 84
ECB , No Padding, 对于输入数据长度不为16的整数倍时,应填充为整数倍
String password = "0123456789abcdef";
byte[] b = password.getBytes("US-ASCII");
SecretKeySpec sks = new SecretKeySpec(b, "AES");
Cipher c = Cipher.getInstance("AES/ECB/NoPadding");
c.init(Cipher.ENCRYPT_MODE, sks);
String plain = "shaofa0012345678";
byte [] input = plain.getBytes();
byte [] output = c.update(plain.getBytes());
//Cipher c2 = Cipher.getInstance("AES/ECB/NoPadding");
c.init(Cipher.DECRYPT_MODE, sks);
byte [] pp = c.update(output);
即使用PKCS5Padding,并不能实质性描述长度问题。PKCS5Padding是说,当长度不为整数倍是,以数字N填充。N = 16 - 实际长度。例如:
实际长度为14,,则填充2个0x02
* * * * * * * * * * * * * * * 02 02
实际长度为1,则填充15个0x0E
* 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E 0E
如果原文刚好末尾有2个02时,就难以区别是填充的值,还是原文的值。