4

I have an SQLite3 database that I need to secure. I'm confused between using sqlcipher to encrypt the whole database that I use in my Electron app or simply encrypt the data using crypto dependency.

Any clarification or explanation would be welcome.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Read the CryptDB where you can find the Oracle Raw database's insecurity on the observer. Your question is not clear and not unanswerable since where the database is stored what are the risks and even what is your data, database scheme, and the queries. – kelalaka Aug 30 '21 at 18:24
  • The database is locally stored. I'm using it within a desktop app. It is a relational DB with simple queries (CRUD). – Oumaima Abou El Mawahib Aug 30 '21 at 19:13

1 Answers1

3

There are two different types of encryption: "encryption at rest" and "row level encryption".

What if someone gets access to your SQLite file? They have all your data. "Encryption at rest" protects you against this by encrypting the SQLite file itself. If someone steals your SQLite file it will be useless to them. sqlcipher provides encryption at rest. This is a good idea in general.

What if someone hacks into your application and injects SQL commands? What if they select all your customer data? It doesn't matter if the file is encrypted, the SQL connection will decrypt it. To protect against this you can add a layer of "row level encryption". This is where your application encrypts the data it writes and decrypts the data it reads. The data being stored is encrypted. This is more complicated and has more performance impacts. Since the data is inserted encrypted, it is more difficult to search and index. Use it only for very valuable data that you're not likely to have to search. You're better off securing your application against SQL injection in general.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • So if I understood, can we combine them both? the encryption at rest and row level encryption? – Oumaima Abou El Mawahib Aug 30 '21 at 18:16
  • 1
    @OumaimaAbouElMawahib You can combine them both, but you should probably be selective about row level encryption as it brings severe performance penalties. Since the inserted data is encrypted it is difficult to search or index. – Schwern Aug 30 '21 at 18:17
  • I have one more question, is encrypting/decrypting the whole database faster than encrypting/decrypting the data (ofc without encrypting the columns needed for search/index)? If I want to execute the CRUD queries, which method is faster ? – Oumaima Abou El Mawahib Sep 11 '21 at 15:16
  • 1
    @OumaimaAbouElMawahib SQLCipher says "Blazing fast performance with as little as 5-15% overhead for encryption", so it's probably faster to use SQLCipher than manually encrypting each column. It's certainly simpler, and simple is important. Encryption overhead is unlikely to be your performance bottleneck, poorly designed queries and schemas are. The cost of encryption is a flat fee whereas the cost of bad queries and bad schemas can balloon rapidly. So don't complicate your queries and schemas with your own encryption. And you can always turn off SQLCipher. – Schwern Sep 11 '21 at 15:29