我从
http://www.ravenblast.com/index.php/blog/android-password-text-encryption/获得了这个代码,尽管它有效,但我越来越怀疑它不够安全.根据其他来源,似乎没有任何初始化向量.
public static String encrypt(String toEncrypt,byte[ ] key) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(key,"AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE,skeySpec); byte[ ] encryptedBytes = cipher.doFinal(toEncrypt.getBytes()); String encrypted = Base64.encodeBytes(encryptedBytes); return encrypted; } public static String decrypt(String encryptedText,"AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE,skeySpec); byte[] toDecrypt = Base64.decode(encryptedText); byte[] encrypted = cipher.doFinal(toDecrypt); return new String(encrypted); }
解决方法
是的,它不是很安全.没有IV,因为没有块链接.
AES算法只能加密128字节的块,无论密钥的大小(它是不相关的).这些块如何链接在一起是另一个问题.最简单的方法是将每个块与其他块分开加密(ECB mode),就像它们是单独的消息一样.我链接的维基百科文章告诉你何时以及为什么这不安全,其他方法(即CBC mode)是首选.
当你做Cipher cipher = Cipher.getInstance(“AES”);您将在ECB mode年获得AES密码.没有直接危险,但如果您的消息具有重复出现的模式,则可能导致以下情况:
原文:加密: