SQLite Context.MODE_PRIVATE

时间:2013-02-13 07:23:12

标签: android sqlite

我想知道:

我们可以在创建数据库时在Context.MODE_PRIVATE中使用SQLite来防止不需要的数据库访问。

我没有在谷歌上得到任何例子 如何在数据库中使用此Context.MODE_PRIVATE 请帮助我提供任何链接或样本。

IN THIS LINK他们在谈论档案。所以数据库也是文件。

我该如何实现?

3 个答案:

答案 0 :(得分:0)

正如commonsware所提到的,内部存储上的SQLite数据库默认是私有的。但正如其他人所提到的那样,根据手机始终可以访问您的文件。

相反,您可以使用任何加密算法将数据保存在数据库中,这将有助于限制可读性,除非入侵者知道加密算法。

您无法在SQLite中设置“Context.MODE_PRIVATE”标志。

答案 1 :(得分:0)

创建数据库时,以下语法很有用

openOrCreateDatabase(String path, int mode, SQLiteDatabase.CursorFactory factory)

例如,

openOrCreateDatabase("StudentDB",Context.MODE_PRIVATE,null);

请参阅this网站上的教程。

答案 2 :(得分:0)

选项1 :使用SQLcipher

选项2 :永远没有机会破解的安全方法。它并不完美,但总比没有好。

  

1)使用此功能插入数据:

public static String getEncryptedString(String message) {
    String cipherText = null;

    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(("YOUR-SECURE-PASSWORD-KEY").getBytes(), "AES"));
        byte[] bytes = cipher.doFinal(message.getBytes());
        cipherText = Base64.encodeToString(bytes, Base64.DEFAULT);
    } catch(Exception ex) {
        cipherText = "Error in encryption";
        Log.e(TAG , ex.getMessage());
        ex.printStackTrace();
    }

    return cipherText;
}
  

2)从数据库获取数据并将其传递给此函数参数:

//This function returns output string 
public static String getDecryptedString(String encoded) {
    String decryptString = null;

    try {
        byte[] bytes = Base64.decode(encoded, Base64.DEFAULT);

        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(("YOUR-SECURE-PASSWORD-KEY").getBytes() , "AES"));
        decryptString = new String(cipher.doFinal(bytes), "UTF-8");
    } catch(Exception ex) {
        decryptString = "Error in decryption";
        ex.printStackTrace();
    }

    return decryptString;
}
  

3)这些方法的好处:   -如果没有正确的密钥,则无法解密。   -AES加密是一种非常安全的加密方法。

     

4)将AES密钥存储在c ++文件中。