java安卓使用CFB
我试图用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);
有人能帮我吗? 非常感谢
# 1 楼答案
假设问题是关于
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/PKCS5Padding对某些数据进行编码的结果与使用AES/CFB/NoPadding对相同数据进行编码的结果不同
为了尽量减少混淆,您应该始终使用显式模式和填充值指定完整转换