Hey there,
I’ve been using sqlcipher since very recently and the thing is, I’ve encountered the 16KB paging requirement and therefore I was lookin for fixes. And I came to find the new long term replacement.
Since im relatevely new to using this library, I wanted to try it out and decided to make some sample applications.
I tried to encrypt my sqlite database with a helper and my imports and including the library was done perfectly but, i still had one issue. In my previous code base i had something like this:
fun initializeDatabase(passphrase:String) { val db = getWritableDatabase(net.sqlcipher.database.SQLiteDatabase.getBytes(passphrase.toCharArray())) db.close() } fun createDatabaseIfNeeded(context: Context, dbName: String, passphrase: String) { try { val dbHelper = DatabaseHelper(context,dbName) dbHelper.initializeDatabase(passphrase) println("Database initialized and tables created successfully.") } catch (e: Exception) { println("Error creating database: ${e.message}") } }
And at time of reading or writing operations I found methods like
getWritableDatabase(key)
getReadableDatabase(key)
Now coming to the issue. I came up with this naive approach( I was experimenting out).
class MyDatabaseHelper(
context: Context,
private val databaseName: String,
private val passphrase: String
) : SQLiteOpenHelper(context, databaseName, null, DATABASE_VERSION)
{
companion object {
private const val DATABASE_VERSION = 1
private const val TABLE_NAME = "my_data"
private const val COLUMN_ID = "id"
private const val COLUMN_TEXT_DATA = "text_data"
private const val COLUMN_TIMESTAMP = "timestamp"
private const val TAG = "MyDatabaseHelper"
}
override fun onCreate(db: SQLiteDatabase) {
val CREATE_TABLE_QUERY = "CREATE TABLE $TABLE_NAME (" +
"$COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"$COLUMN_TEXT_DATA TEXT," +
"$COLUMN_TIMESTAMP INTEGER DEFAULT 0)"
db.execSQL(CREATE_TABLE_QUERY)
Log.d(TAG, "Table '$TABLE_NAME' created.")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
Log.d(TAG, "Upgrading database from version $oldVersion to $newVersion")
when (oldVersion) {
1 -> {
val ADD_TIMESTAMP_COLUMN = "ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_TIMESTAMP INTEGER DEFAULT 0"
db.execSQL(ADD_TIMESTAMP_COLUMN)
Log.d(TAG, "Migration: Added $COLUMN_TIMESTAMP to $TABLE_NAME")
}
}
}
fun insertData(data: String): Long {
val db = this.writableDatabase//getWritableDatabase(passphrase)
val contentValues = ContentValues().apply {
put(COLUMN_TEXT_DATA, data)
put(COLUMN_TIMESTAMP, System.currentTimeMillis())
}
val result = db.insert(TABLE_NAME, null, contentValues)
db.close()
Log.d(TAG, "Inserted data: '$data', Result: $result")
return result
}
fun getAllData(): List<Pair<String, Long>> {
val dataList = mutableListOf<Pair<String, Long>>()
val db = readableDatabase//getReadableDatabase(passphrase)
val cursor = db.rawQuery("SELECT $COLUMN_TEXT_DATA, $COLUMN_TIMESTAMP FROM $TABLE_NAME", null)
cursor.use {
if (it.moveToFirst()) {
val textColumnIndex = it.getColumnIndex(COLUMN_TEXT_DATA)
val timestampColumnIndex = it.getColumnIndex(COLUMN_TIMESTAMP)
if (textColumnIndex != -1 && timestampColumnIndex != -1) {
do {
val textData = it.getString(textColumnIndex)
val timestamp = it.getLong(timestampColumnIndex)
dataList.add(Pair(textData, timestamp))
} while (it.moveToNext())
}
}
}
db.close()
Log.d(TAG, "Retrieved data: $dataList")
return dataList
}
}
and turns out the helper was doing its thing but when I downloaded my db file I found out that it was never encrypted. Did i miss something? Please tell me if I need to change my approach to solve this.