Make a copy of a database to your memory.

JvdWJvdW NLMember

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?

Best Answer

Answers

  • HunumanHunuman GBMember ✭✭✭✭
    edited October 2016

    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

  • JvdWJvdW NLMember
    edited October 2016

    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?

  • JvdWJvdW NLMember
    edited October 2016

    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?

  • HunumanHunuman GBMember ✭✭✭✭

    @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

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

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

  • JvdWJvdW NLMember

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

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

    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

  • JvdWJvdW NLMember

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

  • ThomasBurkhartThomasBurkhart DEMember ✭✭✭✭

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

  • JvdWJvdW NLMember

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

  • JvdWJvdW NLMember

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

Sign In or Register to comment.