title: Remove SQLCipher encryption from my existing application's database
body: <p>I have an Android application which uses SQLCipher for database encryption. The application has gone live and has many active users. I'm looking for a solution which can remove the SQLCipher encryption from application's existing database without loosing user's data.</p>
  
  <p>I tried doing the inverse of what is mentioned in <a href="https://stackoverflow.com/questions/29860873/using-sqlcipher-with-android/29867682#29867682">this</a> post but unable to open my encrypted database file.</p>
  
  <pre><code>public static void decrypt(Context ctxt, String dbName, String passphrase)
          throws IOException {
      try {
  
          File originalFile = ctxt.getDatabasePath(dbName);
  
          int version = 0;
          if (originalFile.exists()) {
              File newFile = File.createTempFile("sqlite", "tmp", ctxt.getCacheDir());
  
              net.sqlcipher.database.SQLiteDatabase dbCipher = net.sqlcipher.database.SQLiteDatabase.openDatabase(
                      originalFile.getAbsolutePath(), passphrase, null,
                      net.sqlcipher.database.SQLiteDatabase.OPEN_READWRITE);
  
              if (dbCipher.isOpen()) {
                  dbCipher.rawExecSQL(String.format(
                          "ATTACH DATABASE '%s' AS plaintext KEY '%s';",
                          newFile.getAbsolutePath(), passphrase));
                  dbCipher.rawExecSQL("SELECT sqlcipher_export('plaintext')");
                  dbCipher.rawExecSQL("DETACH DATABASE plaintext;");
  
                  version = dbCipher.getVersion();
  
                  dbCipher.close();
              }
  
  
              SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(newFile, null);
              db.setVersion(version);
              db.close();
  
              originalFile.delete();
              newFile.renameTo(originalFile);
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>
  
  <p>And, here are the error logs I got...</p>
  
  <pre><code>06-04 11:33:54.929: E/SQLiteLog(12309): (26) file is encrypted or is not a database
  06-04 11:33:54.929: E/DefaultDatabaseErrorHandler(12309): Corruption reported by sqlite on database: /data/data/ril.jio.protrak/cache/sqlite1817652413tmp
  </code></pre>