locked
Make a copy of a database to your memory. RRS feed

  • Question

  • User269765 posted

    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?

    Friday, October 28, 2016 12:55 PM

Answers

  • User66163 posted

    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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 3, 2016 4:07 PM

All replies

  • User66163 posted

    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

    Friday, October 28, 2016 11:11 PM
  • User269765 posted

    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:

         MemoryStream inmemory = new MemoryStream();
         using (FileStream f = File.Open(path, FileMode.Open))
         {
             f.CopyTo(inmemory);
         }
    

    but that doesn't seem to work either. Is there a better way of reading the file into memory?

    Monday, October 31, 2016 7:39 AM
  • User269765 posted

    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

            MemoryStream inmemory = new MemoryStream();
            using (FileStream f = File.Open(path, FileMode.Open))
            {
                f.CopyTo(inmemory);
            }
    

    But that also didn't seem to work. Is there a better way to copy files into memory?

    Monday, October 31, 2016 8:28 AM
  • User66163 posted

    @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

    Tuesday, November 1, 2016 1:31 PM
  • User179286 posted

    @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.

    Tuesday, November 1, 2016 10:15 PM
  • User269765 posted

    @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?

    Wednesday, November 2, 2016 3:10 PM
  • User179286 posted

    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

    Wednesday, November 2, 2016 3:25 PM
  • User269765 posted

    @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);)

    Thursday, November 3, 2016 10:19 AM
  • User179286 posted

    why do you want to copy it to memory when you just want to open it? This won't work! Sqlite expects a File

    Thursday, November 3, 2016 10:26 AM
  • User269765 posted

    @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)

                FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read);
                MemoryStream ms = new MemoryStream();
                await Crypto.Decode(input, ms, password);
    

    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.

    Thursday, November 3, 2016 12:36 PM
  • User66163 posted

    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

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, November 3, 2016 4:07 PM
  • User269765 posted

    @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.

    Friday, November 4, 2016 7:28 AM