这种AES加密是否足够安全?

时间:2013-02-18 12:52:10

标签: java security encryption aes block-cipher

我从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, byte[ ] key) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "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);
}

1 个答案:

答案 0 :(得分:9)

是的,它不是很安全。没有IV,因为没有块链接

AES算法只能加密128字节的块,无论密钥的大小(它是不相关的)。这些块如何链接在一起是另一个问题。最简单的方法是将每个块与其他块(ECB mode)分开加密,就像它们是单独的消息一样。我链接的维基百科文章告诉你何时以及为什么这不安全,其他方法(即CBC mode)是首选。

当您执行Cipher cipher = Cipher.getInstance("AES");时,您将获得ECB mode中的AES密码。没有直接危险,但如果您的消息具有重复出现的模式,则可能导致以下情况:

原文:enter image description here已加密:encrypted

相关问题