Retrieving data from an encrypted text file?
Let's say I have a text file contains my data.
data :
ab
bc
de
-
encrypted data on text file :
ba
cb
ed
I want to find bc
from text file, so I have to decrypt the text file with this code :
SL:=TStringList.create;
SL.Load开发者_开发问答FromFile(textfile)
SLtemp:=TStringList.create;
for I := 0 to SL.Count - 1 do
SLtemp.Add(ReverseString(SL[i])); //decrypt
SL.Free;
for I := 0 to SLtemp.Count - 1 do
if SLtemp[i] = 'bc' then
begin
showmessage('found');
break;
end;
SLtemp.Free;
I think my way is wasting resources. I have to load whole file to memory and decrypt them. I need some suggestions here to find quickly a specific line from an encrypted text.
Thanks.
Sounds like you're trying to implement a database of some sort. SQLite has transparent encryption support with indexes. Trust me, your database is going to suck unless you put man-years into it.
To actually fix the problem, you have to build (encrypted) indexes by decrypting every line and indexing them. You should be very familiar with indexing strategies if you're working with data with performance in mind.
But first ask yourself, is the data large enough to even matter? If it runs in a tenth of a second, making it run in a hundredth is pointless. If it takes a week, that's a sign that you're hopelessly on the wrong track anyway, and small optimizations won't help much.
Sorry if this is a bit harsh, especially coming from someone who's implemented a few homebrew databases.
Unless you intentionally separate the encryption blocks for each element you wish to search for (which is a perfectly viable option), you will have to at least decrypt half of the file each time (on average).
You can either encrypt line-by-line (like I said, perfectly possible), encrypt your search text, and then simply find the line that matches.
Otherwise you will have to decrypt the file and search the output of the decryption process for the search string. If you can search the output while the encryption is still in progress, you can probably stop the decryption early when you find the text.
I like the encrypted database idea from SilverbackNet, and I agree you shouldn't roll your own. You may also need to ensure the index is encrypted as well, if the database doesn't take care of that detail for you (it should)
If your encryption process is for one line a time. Why don't you encrypt your keyword "bc" to "cb", then look up it? (without decrypt the whole file)
精彩评论