【问题标题】:How to store an AES password in a SQLite database in Android?如何将 AES 密码存储在 Android 的 SQLite 数据库中?
【发布时间】:2015-07-13 07:09:16
【问题描述】:

我正在尝试使用以下代码将密码存储在 SQLite 数据库中:

public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encVal);
    return encryptedValue;
}

public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
}

并将这个字节数组作为键

private static final byte[] keyValue = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
            'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

但问题在于该密钥的存储位置,因为使用该密钥,任何人都可以解密数据并导致安全问题..

请帮助我..提前谢谢..

【问题讨论】:

    标签: android sqlite encryption aes


    【解决方案1】:

    使用用户生成的密钥,例如 pin 屏幕。当应用程序启动时向用户询问密钥,您将不需要存储它,然后将其保存在内存中直到应用程序关闭。

    【讨论】:

      【解决方案2】:

      无论您将密钥放在哪里。如果我可以反编译并运行您的代码,我唯一要做的就是在创建密钥后的一行中放置断点。没有工作方法可以将加密数据放入您的代码中并使其仅可用于您的代码。

      【讨论】:

        猜你喜欢
        • 2011-12-20
        • 1970-01-01
        • 2023-02-07
        • 2011-02-27
        • 2011-07-15
        • 2017-09-25
        • 1970-01-01
        • 2016-02-15
        • 2012-03-16
        相关资源
        最近更新 更多