Delete Encrypted Couchbase Lite DB (Xamarin) - encryption

I'm using Couchbase Lite for Xamarin (android) and I would like to delete an encrypted DB.
Here is my code to delete:
var databaseName = "XYZ";
var manager = Manager.SharedInstance;
manager.GetExistingDatabase(databaseName).Delete();
When I run this code I get:
Couchbase.Lite.CouchbaseLiteException: Error 21, 26 (file is encrypted or is not a database) executing sql 'CREATE TABLE docs ( doc_id INTEGER PRIMARY KEY, docid TEXT UNIQUE NOT NULL)'
I know the database exists and is encrypted.
The docs don't specify that deleting only works on unencrypted databases.
http://developer.couchbase.com/documentation/mobile/1.3/develop/references/couchbase-lite/couchbase-lite/database/database/index.html
Is there a different way to delete an encrypted database?

The correct way to do this is instead of
manager.GetExistingDatabase(databaseName).Delete();
do
manager.DeleteDatabase(databaseName);
This allows for deletion of an encrypted couchbase lite DB without having the key.

Related

Why is my managed instance database encryption is not being disabled?

I am trying to disable encryption on a database which is hosted inside Azure managed instance. I am not able to disable the encryption for it. Any help would be highly appreciated.
I have tried this alter database query:
ALTER DATABASE "DATABASE-NAME" SET ENCRYPTION OFF
I had checked encryption on each database as:
SELECT name, is encrypted
FROM sys.databases;
ALTER DATABASE "DATABASE-NAME" SET ENCRYPTION OFF
The query I used to alter the database turns out run successfully but the encryption is not disabled.
It may take some time to complete. On databases of less than 100 GB you may find it takes 20-25 minutes to complete.
You can monitor de progress using below query:
SELECT DB.NAME, DEK.ENCRYPTION_STATE, DEK.PERCENT_COMPLETE
FROM SYS.DM_DATABASE_ENCRYPTION_KEYS AS DEK
FULL JOIN SYS.DATABASES AS DB
ON DB.DATABASE_ID = DEK.DATABASE_ID
ORDER BY DB.NAME

How encrypt SQLite database with FireDAC?

I'm create SQLite databse with DB Browser for SQLite (non encrypted) and open with FireDAC in delphi.(Can retrive data Eg. Select * from abc).
How encrypt this SQLite database with FireDAC? When enter username, password and encrypt get message "Cipher DB is not encrypdet"
Note:
When create SQLite database from Delphi FireDac I can use encryption!
To encrypt a database, use a TFDSQLiteSecurity Component. You'll also need a TFDSQLitePhysSQLiteDriverLink component to go along with it.
If a database is unencrypted, then its password is ''. So use '' as the OldPassword and create the new password in that case. Passwords are formatted as algorithm:PassPhrase. See documentation on the choices, I use aes-256. Also, the database needs to be closed when you do this.
...
//Change password
FDSQLiteSecurity1.Password := OldPassword;
FDSQLiteSecurity1.ToPassword := NewPassword; // example: 'aes-256:mypassword123'
FDSQLiteSecurity1.ChangePassword;
...
//Remove Password
FDSQLiteSecurity1.Password := OldPassword;
FDSQLiteSecurity1.ToPassword := '';
FDSQLiteSecurity1.RemovePassword;
...
From the Documentation
SQLite Encrypted Database
Approach
One of the distinctive SQLite
features is the high-speed strong database encryption. It allows you
to make database file content confidential and enforce integrity
control on the database file. The encrypted database format is not
compatible with other similar SQLite encryption extensions. This means
that you cannot use an encrypted database, encrypted with non-FireDAC
libraries. If you need to do this, then you have to decrypt a database
with an original tool and encrypt it with FireDAC.
Recent Delphi versions come with an example project for working with encryption on Sqlite databases, see this documentation. I have not used this myself, btw.
It includes this section
Encrypt DB
Encrypt: Encrypts the database according to the Encryption mode and the password provided.
The sampe uses TFDSQLiteSecurity.SetPassword to encrypt the database with the password provided.
The database password is the combination of <encryption algorythm>:<password>.
I have faced several challenges when first time tried to encrypt SQLite database for use with Embarcadero FireDAC. Also all information is published by Embarcadero question pops up again and again on different forums. My case was solved based on community support, but when time has permitted simple Delphi application was assembled and available on Sourceforge. Hope it will make encryption/decryption slightly easier particularly for the newbie
https://sourceforge.net/projects/sqlite-sequrity-for-delphi/

Open encrypted SQLite database with SQLiteStudio

I'm using System.Data.SQLite to create an encrypted SQLite database in a WPF project. The encryption is done by providing a password in the connection string just like this one:
new SQLiteConnection("Data Source=database.sqlite;Password=123456;Version=3;New=false;Compress=True;);
This works well but since the database is encrypted now I'm not able to open it anymore with tools like DB Browser for SQLite or SQLiteStudio. SQLite Studio offers the possibility to open a database of type SQLCipher but I'm struggling to provide the correct settings for my database to open.
Does anybody know how to open an encrypted SQLite database, so I can inspect the data in it?
I was the same problem with SQLiteStudio, in my case, I resolved using the following configuration for SQLChiper:
PRAGMA kdf_iter = '64000';
PRAGMA cipher_page_size = 1024;
PRAGMA cipher_hmac_algorithm = HMAC_SHA1;
PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1;
On the SQLiteStudio would be like:
For extra configuration or other possible parameters, you could visit the following link https://www.zetetic.net/sqlcipher/sqlcipher-api/

SQLite INSERT or UDATE with a custom condition

I know there is a lot of question already asked and answered on this subject but they don't seem to fit my situation.
I have a distant sqlite database — DB server — and a local one — DB local containing photo album entries. DB local updates whenever needed from DB server. DB server has a primary key called identifier, which is stored in DB local to prevent duplicates, but DB local also has its own primary key column called id
If I need to create a new album on my phone I insert an entry in DB local with identifier set to -1 and when DB server will be reachable ask for a proper identifier.
My issue is : I do a lot of refresh and don't want to increment my primary key each time.
When I refresh DB local from DB server I would like to INSERT new albums, and UPDATE existing ones.
I read about the INSERT OR REPLACE statement but it would require my identifier column in DB local to be set as unique. Unfortunately I cannot do so since I can have multiple identifierset to -1.
Is there any way to perform an INSERT or UPDATE conditionally in a single request ?
Thanks !
EDIT : the update is done this way : the DB local is updated from DB server. DB local data is never pushed to DB server, the only way to add a new item is to call an API on the server which will create an empty entry on DB server and get its identifier. But since server is not always reachable (EDGE/3G) some entries in DB local have identifier set to -1. Once the API call have returned with the corresponding identifier we store it instead of -1 for the corresponding entry in DB local

Equivalent of sha1 in db2

I've stored password in mysql using SHA1 encryption And I have migrated my DB from mysql to DB2.
From Db2, how can I fetch record from Db2?
Select * from table where name = 'user' and password = sha1('123456')
But it generates error in db2. How can I check the login for the existing details?
If the original app contains only a few references to sha1() in the SQL statements, you could switch them a similar function in DB2, such as ENCRYPT() http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0004211.html.
Now you have a HASH function
SELECT HEX(HASH('Charlie at IBM',1)) FROM SYSIBM.SYSDUMMY1;
D6E42303462491FC696EAC53C1B086A5034735A7

Resources