Hi,
I'm trying to make a copy of an existing datbase file to my memory.
The original database file is encrypted, I want to copy it to my memory to decrypt it there, insert or read the data i need, encrypt it again and overwrite the original database file. This because if the app crashes you won't be able to see any un-encrypted data. (maybe there is a better way, if you have one please tell me because i couldn't think of one.)
My code so far:
var dbName = "Dlgtest.db3"; var something = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); var path = System.IO.Path.Combine(something, dbName); var conn = new SQLiteConnection(path); var connection = new SQLiteConnection(":memory:");
I know you can use SQLCipher to encrypt the file, but they don't support UWP which i need.
Is there a way to write from the var conn to the var connection?
Hi @JvdW,
The standard way to do this is to:
1. Encrypt the data in memory before writing into the database.
2. When you read the data into memory, you have to decrypt it before using.
This is as secure as your approach, but much more memory efficient.
Not sure why you would want to do it any other way....
Tim
Answers
Hi @JvdW
If the database is encrypted at the file level, simply read the file into memory and decrypt.
If the database is encrypted at the record/field level, you will need to read the individual records and write them to the new DB, using SQL.
Hope this helps.
Tim
Hey Tim, thanks for your answer.
When i try to copy the file into memory using:
File.Copy(path, ":memory:");
i get this exception: System.UnauthorizedAccessException: Access to the path "/data/data/SQLiteProject/files/Dlgtest.db3" or ":memory:" is denied.
I also tried:
but that doesn't seem to work either.
Is there a better way of reading the file into memory?
Hey @Hunuman, Thanks for your reply
When i try to read the file into memory using:
File.Copy(path, ":memory:");
I get the exception: System.UnauthorizedAccessException: Access to the path "/data/data/SQLiteProject/files/Dlgtest.db3" or ":memory:" is denied.
I also tried
But that also didn't seem to work.
Is there a better way to copy files into memory?
@JvdW
Why are you trying to copy to the file to memory!
Just open the file and read it!
Once you have read its contents its in memory!
Tim
@JvdW Your Approach with the MemoryStream should work fine. Why do you think it didn't work? Remember to seek the MemoryStream to it's start before you read from it.
@ThomasBurkhart Thanks for your reply, I now have the file in the MemoryStream. But I can't figure out how to read the database out of the MemoryStream. Is that even possible?
You mean reading it byte by byte? Either call
.ToArray https://msdn.microsoft.com/de-de/library/system.io.memorystream.toarray(d=printer,v=vs.110).as PC or Get buffer or check the StreamReader classes
@ThomasBurkhart I meant by putting the whole .DB3 file into the memorystream and creating a SQLite Connection from that file. ( like
var connection = new SQLiteConnection(Path of memorystream);
)why do you want to copy it to memory when you just want to open it? This won't work! Sqlite expects a File
@ThomasBurkhart The .DB3 file that is on the disk is encrypted. I wanted to copy it to the memory -> unencrypt it -> read the database from that memoryfile (like this)
thanks to your explanation I understand now that this won't work.
Since I work on an app with confidential data I can't allow that there is a possibility that the unencrypted database is read. This is why I wanted it in the memory, because if the app closes the memory gets deleted. (with the unencrypted database).
Since I can't copy the file into the memory i will have to find a different approach for this problem.
Hi @JvdW,
The standard way to do this is to:
1. Encrypt the data in memory before writing into the database.
2. When you read the data into memory, you have to decrypt it before using.
This is as secure as your approach, but much more memory efficient.
Not sure why you would want to do it any other way....
Tim
@Hunuman You are right, thanks.
I tought it wouldn't be efficient to encryt each record itself but instead encrypt the whole database file.
But the way I transfer the data from the file to the memory now, I have to read it record by record. So encrypting and decrypting it wouldn't make a huge difference.