6

我从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);
}
4

1 回答 1

9

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

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

当你这样做时,你会得到一个ECB 模式下Cipher cipher = Cipher.getInstance("AES");的 AES 密码。没有直接的危险,但如果您的消息具有重复模式,则可能导致以下情况:

原件:在此处输入图像描述加密:加密

于 2013-02-18T12:57:22.383 回答