Read / write to the same file - WeOnlyDo Discussion board

Read / write to the same file (General questions)

by dandraka, Monday, February 21, 2005, 14:00 (7026 days ago)

Hello again,

While encrypting (or decrypting), is it possible to read from and write to the same file ?

Our situation is like this: There is a disk file which is an encrypted database. It can grow large enough, so the stuff that need to be done (decrypt it, update some records and then encrypt it again) cannot be done in-memory.

So far we are using an encryption component (which has other problems, so we want to get rid of it) that is able to encrypt/decrypt using a single file. For example, at the middle of the encryption process, the file is half decrypted, half encrypted.

If the process would stop right in the middle for some reason (which BTW we have no way of knowing with the current component; this is its main problem, it doesn't throw an exception or return an error code, nothing) then the database would be entirely corrupt. In our case, this is a nice thing to have. Why ? Because if the process fails, the user cannot use the (corrupted) database. This very db is what we're trying to protect from the eyes of the user.

We have, of course, thought about writing the decrypted file to the disk and then (after we update the records and encrypt it again) deleting it. But this would leave the decrypted file, even for a few seconds, somewhere where a user can copy it.

Is wodCrypt capable of something similar ? Or, alternatively, can you think of a possible solution for our case ?

Thanks in advance,

Dimitris Andrakakis

Re: Read / write to the same file

by wodSupport, Monday, February 21, 2005, 15:38 (7026 days ago) @ dandraka

Dimitry,

I don't think this will work. wodCrypt doesn't read entire file, it reads part by part. While I'm thinking about it, it would make sense to allow it to partially encrypt it, since encryption is based on same file chunks, but still I doubt it will work.

Why don't you encrypt to MemBlob, and then write it to a file afterwards?

Re: Read / write to the same file

by dandraka, Monday, February 21, 2005, 15:49 (7026 days ago) @ wodSupport

Dimitry,

I don't think this will work. wodCrypt doesn't read entire file, it reads part by part. While I'm thinking about it, it would make sense to allow it to partially encrypt it, since encryption is based on same file chunks, but still I doubt it will work.

It doesn't, I've tested it [:sad:] It leaves me with an empty (zero bytes) file.

Why don't you encrypt to MemBlob, and then write it to a file afterwards?

That was one thing I tried, and it's OK indeed --as long as the file size is reasonable. But the file (as I said, it's a database) can well reach 500-600MB (and, rarely, maybe even 1GB). It's not like that most of the time, but it will happen.

What will happen then ? I suppose Windows will use the swap file like crazy, and on slower machines (sadly some clients still have, like, Pentium 2's with 128MB ram and Win98 [:sad:] ) this may cause hangs / BSODs / whatever.

Re: Read / write to the same file

by wodSupport, Monday, February 21, 2005, 19:54 (7026 days ago) @ dandraka

Yes, I understand completely. And, unfortunatelly, I must say I don't have answers for you.

If we try to go into changing, this will not turn out good. Using two FileBlobs accessing same file and each of them being carefull not to destroy contents of other FileBlob... Urgh... No, I'd rather not go into that direction, let's figure some workaround :)

Re: Read / write to the same file

by dandraka, Tuesday, February 22, 2005, 16:56 (7025 days ago) @ wodSupport

No, I'd rather not go into that direction, let's figure some workaround.

Any ideas yet ? I've put some work into this, but I can't make anything work. Then again, I'm far from a Delphi guru [:smile:]

Thanks for your time,

Dimitris Andrakakis

Re: Read / write to the same file

by wodSupport, Tuesday, February 22, 2005, 19:54 (7025 days ago) @ dandraka

Hmm, not really. Only thing that came to my mind is to use MemBlob.

You could also play a bit perhaps, and split original file to chunks, encrypt it, and then write back. So, you would take care of splitting and then you don't care where wodCrypt reads/writes. Would that work?

Re: Read / write to the same file

by Traveller, Sunday, May 26, 2013, 14:23 (4010 days ago) @ wodSupport

Hi dandraka,
I think I have a simple Solution for you:

1- Open your Source File decrypt it, Read Line by Line or in Chunks.
2- Mean While you should have Opened anew File for Writing Purpose and immidiately
write the previous chunks to this file untile your source file will comes to
an end.
3- After this step Now add your New Records by writting them or say appending them
Until you come to an end too, Then Close File both.
4- Rename your Course File to BB.dat.
5- rename the New Written File Containing old And new Records ... Updated to the Old
Name of your Source File.
6- Now Encrypt it.

Test the Above Idea on A Sample Dbase Until you become Sure it Works, The Apply to
Your Work.

Best Regards.