【问题标题】:encryption of an already existing database for SQLCipher为 SQLCipher 加密已经存在的数据库
【发布时间】:2012-12-20 09:42:56
【问题描述】:

我在我的应用程序中使用了一个已经存在的数据库(另请参阅1)。我使用 Java 应用程序加密了数据库。 在我的应用程序中,我尝试使用以下代码读取 encrypted_database,但我得到 SQLiteException: file is encrypted or is not a database

    SQLiteDatabase.loadLibs(mContext);
    SQLiteDatabase dataBase = SQLiteDatabase.openDatabase(mPath, mPassword, null, SQLiteDatabase.OPEN_READONLY);
    String query = "Select distinct _id from TABLE";
    Cursor cursor = dataBase.rawQuery(query, null);
    return cursor;

我已经用 SQLCipher 加密了我的数据库,我也可以读取数据,所以一切正常。

SQLCipher 和现有数据库的问题是我必须将完整的 unencrypted_database 复制到 encrypted_database。当我在手机上执行此操作时,这需要很长时间。

我的想法是:用 java 编写一个加密数据库的应用程序,并在您的应用程序中使用这个 encrypted_database。结果是我只需要在我的应用程序中打开已经存在的 encrypted_database 并且不需要复制。

现在我写了一个 Java 应用程序(基于2,3)但仍然存在一些与 SQLCipher 及其设计相关的问题(4):

  • 如何在数据库页面中划分我的数据库?在4 中,数据库页面仅由其大小(1024 字节)定义。但是我必须在我的 encrypted_database 文件中写入“数据库页面开始”或“数据库页面结束”
  • salt 和随机初始化向量 (iv) 是 1024 字节的一部分吗?

    public static void main(String[] args) throws Exception{
    
        outFile_enc = new FileOutputStream(mFileNameEncrypted);
        outFile_dec = new FileOutputStream(mFileNameDecrypted);
    
        int keyLength = 256;
        // salt
        salt = new byte[16];
        Random rnd = new Random();
        rnd.nextBytes(salt);
        int iterations = 4000;
    
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(mPassWord.toCharArray(), salt, iterations, keyLength);
        SecretKey passwordKey = keyFactory.generateSecret(keySpec);
        key = new SecretKeySpec(passwordKey.getEncoded(), "AES");
    
        // creates a cipher and init it for encryption
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
    
        AlgorithmParameters params = cipher.getParameters();
        iv = params.getParameterSpec(IvParameterSpec.class).getIV();
    
        encryptData(cipher);            
    }
    
    public static void encryptData(Cipher cipher) throws Exception{
        // File to encrypt
        inFile = new FileInputStream(mFileName);
    
        // unique random salt in the first 16 bytes of the file
        outFile_enc.write(salt);
    
        // Read file and encrypt its bytes
        byte[] input  = new byte[64];
        int bytesRead;
        while((bytesRead = inFile.read(input)) != -1){
        byte[] output = cipher.update(input, 0, bytesRead);
        if(output != null)
            outFile_enc.write(output);
        }
    
        byte[] output = cipher.doFinal();
        if(output != null)
            outFile_enc.write(output);
        // random initialization vector is stored at the end of a page
        outFile_enc.write(iv);
    
        inFile.close();
        outFile_enc.flush();
        outFile_enc.close();    
    }
    

感谢每一个帮助/想法/评论 :)

【问题讨论】:

    标签: java android database encryption sqlcipher


    【解决方案1】:

    不建议尝试从头开始重新创建 SQLCipher 文件的方法。该格式比您正在执行的操作更复杂,并且重现一个有效的 SQLCipher 文件将非常重要。相反,您应该只使用SQLCipher command line programencrypt your database 进行分发。

    【讨论】:

    • 感谢您的回答。我看到了 SQLCipher API,但我不明白如何解决我的问题。您的回答帮助我了解了一个想法:)。下次我试试。
    猜你喜欢
    • 2012-11-05
    • 2015-04-10
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多