# Connecting to decrypted SQLite database

**URL:** <https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549>\
**Category:** Databases\
**Created:** [July 29, 2018, 5:46am UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549 "2018-07-29T05:46:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerson\_Nerger](https://forum.xojo.com/user_avatar/forum.xojo.com/gerson_nerger/32/1225_2.png) [@Gerson\_Nerger](https://forum.xojo.com/u/Gerson_Nerger)\
**Post date:** [July 29, 2018, 5:46am UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/1 "2018-07-29T05:46:53Z")

</div>

Hi,

with this code below I connect to an encrypted database and use it without problems:

[code]dim pdb as new SQLiteDatabase  
dim pdbsql as string

pdbsql = “SELECT … sql blabla … not the problem”

dim datapdb as RecordSet

pdb.DatabaseFile = GetFolderItem(“performance\_database.sqlite”)  
pdb.EncryptionKey=“mysecretpassword”

if pdb.connect then  
pdb.Decrypt  
datapdb=pdb.sqlselect(pdbsql)  
If datapdb \<\> Nil Then  
if datapdb.field(“intV1”).value = Nil then  
lblTakeoffSpeeds.bold = true  
lblTakeoffSpeeds.textcolor = &cFF0000  
lblTakeoffSpeeds.text =“T/O NOT ALLOWED!”  
else  
lblTakeoffSpeeds.bold = true  
lblTakeoffSpeeds.textcolor = &c00FF00  
lblTakeoffSpeeds.text=("V1 = " + datapdb.Field(“intV1”).Value + " / " + "VR = " + datapdb.Field(“intVR”).Value + " / " + "V2 = " + datapdb.Field(“intV2”).Value + " / " + "VFS = " + datapdb.Field(“intVFS”).Value)  
datapdb.Close  
end if  
pdb.encrypt(“mysecretpassword”)  
end if  
else  
MsgBox("Connection error: " + pdb.ErrorMessage)  
end if  
[/code]

But if the database is already decrypted I get the error message “Connection error: file is encrypted or is not a database.” … both is wrong: the database is decrypted (because I can open it and work with it with DB Browser for SQLite) and it is definitely a sqlite database.

Because of the connection error I am not able to encrypt the database again before using it …

How can I make sure that a normally encrypted database can be encrypted again when it was left decrypted ???

Thank you very much in advance,  
Gerson Nerger

---

<div class="post-metadata">

**Author:** ![Stefan\_Cyrus](https://forum.xojo.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@Stefan\_Cyrus](https://forum.xojo.com/u/Stefan_Cyrus)\
**Post date:** [July 29, 2018, 6:51am UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/2 "2018-07-29T06:51:14Z")

</div>

I tried to read a DB encrypted with XOJO and couldn’t open this DB with “DB Browser for SQLite”.

I think both use different methods fo encrypting a SQLite DB.

XOJO uses SEE ([https://www.hwaci.com/sw/sqlite/see.html](https://www.hwaci.com/sw/sqlite/see.html) , a $2000 License Modul)

Stefan Cyrus

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [July 29, 2018, 7:28am UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/3 "2018-07-29T07:28:43Z")

</div>

You don’t need to decrypt the database in order to use it. Simply supply the encryption key and connect.

Your best option is to try to connect without the encryption key first. If that works, you know it wasn’t encrypted.

Something like:

[code]Const EncryptionKey = “TheSecretPassword”  
Dim Connection As New SQLiteDatabase  
Connection.DatabaseFile = FolderItem  
If Connection.Connect Then  
Connection.Encrypt(EncryptionKey)  
Else  
Connection.EncryptionKey = EncryptionKey  
If Not Connection.Connect Then  
// Wrong encryption key? Throw exception?  
Return  
End If  
End If

// Now connected to an encrypted database that is ready for use  
[/code]

You could reverse the logic too and attempt to connect with encryption first, you would just need to clear the .EncryptionKey property before trying to connect without encryption.

---

<div class="post-metadata">

**Author:** ![Gerson\_Nerger](https://forum.xojo.com/user_avatar/forum.xojo.com/gerson_nerger/32/1225_2.png) [@Gerson\_Nerger](https://forum.xojo.com/u/Gerson_Nerger)\
**Post date:** [July 29, 2018, 7:59am UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/4 "2018-07-29T07:59:41Z")

</div>

@Thom McGrath Thank you very much for your reply. This answered my question. I thought decrypting is necessary to use an encrypted database.

---

<div class="post-metadata">

**Author:** ![Ralph\_Alvy](https://forum.xojo.com/user_avatar/forum.xojo.com/ralph_alvy/32/4528_2.png) [@Ralph\_Alvy](https://forum.xojo.com/u/Ralph_Alvy)\
**Post date:** [July 29, 2018, 10:03pm UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/5 "2018-07-29T22:03:54Z")

</div>

If you decrypt it during connecting to it, any other passerby with access to that folder will find an unencrypted database. That goes against the reason you encrypted the database to begin with.

---

<div class="post-metadata">

**Author:** ![Gerson\_Nerger](https://forum.xojo.com/user_avatar/forum.xojo.com/gerson_nerger/32/1225_2.png) [@Gerson\_Nerger](https://forum.xojo.com/u/Gerson_Nerger)\
**Post date:** [July 30, 2018, 3:13pm UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/6 "2018-07-30T15:13:16Z")

</div>

I agree.

---

<div class="post-metadata">

**Author:** ![Ulrich\_Bogun](https://forum.xojo.com/user_avatar/forum.xojo.com/ulrich_bogun/32/1864_2.png) [@Ulrich\_Bogun](https://forum.xojo.com/u/Ulrich_Bogun)\
**Post date:** [July 30, 2018, 4:14pm UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/7 "2018-07-30T16:14:51Z")

</div>

There may be uses for a temporary decryption. I did so with a project where I had to examine the database, and my preferred db editor does not give me the option to connect to encrypted SQLite databases.  
So very much like Thom wrote, I try to open the db encrypted and if successful, during a debug run I decrypt it and connect again. Vice versa in a build.

It should be noted that I worked on local copies of the database, so there is no risk of others having access to the decrypted file.  
In any other case, its surely better to keep it encrypted all the time.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 7:18pm UTC](https://forum.xojo.com/t/connecting-to-decrypted-sqlite-database/44549/8 "2020-10-29T19:18:05Z")

</div>


