我正在尝试用AES和CFB对我的字符串进行编码。如果我这样做了
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");,它运行良好。但是如果我使用"AES/CFB/NoPadding“而不是"AES",那么具有相同密码的相同字符串是不同的。以下是我的代码:
SecretKeySpec key = new SecretKeySpec(cryptPassword.getBytes(), "AES");
byte[] cryptByte = cryptString.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] hans = cipher.doFinal(cryptByte);
cryptString = Base64.encodeToString(hans,Base64.DEFAULT);有人能帮我吗?非常感谢!
发布于 2014-04-16 14:09:07
假设问题是关于Cipher.getInstance("AES")和Cipher.getInstance("AES/CFB/NoPadding")的区别
当您没有在转换字符串中指定它们时,对于Oracle JDK the default mode/padding来说是“ECB/PKCS5Padding.”,这意味着Cipher.getInstance("AES")与Cipher.getInstance("AES/ECB/PKCS5Padding")相同。
使用AES/ECB/PKCS5Padd对某些数据进行编码的结果与使用AES/CFB/NoPadding对相同数据进行编码的结果是可以预见的不同。
为了最大限度地减少混淆,您应该始终使用显式模式和填充值指定完整转换。
https://stackoverflow.com/questions/23087482
复制相似问题