views:

124

answers:

2

For example, if I have three ASCII files:

file1.txt
file2.txt
file3.txt

...and I wanted to combine them into one encrypted file:

database.txt 

Then in the application I would decrypt the database.txt and put each of the original files into a 'File' class on the heap:

class File{
public:
    string getContents();
    void setContents(string data);
private:
    string m_data;
};

Is there some way to do this?

Thanks

+6  A: 

Just use a zip file?
You can of course roll your own header meta data to store the filenames, but this particular wheel has been re-invented enough times.

If you need better encryption than that provided by zlib, then you can either use the crypt fucntions in your platform, or it's very easy to implement something like blowfish

Martin Beckett
+1 for suggesting that existing technology be used.
Indeed; probably the best solution, unless there are licensing issues or hardware constraints.
strager
A: 

If you don't want to use a pre-existing file format, make your own simple one.

Something like this layout should work:

class DatabaseFile {
    private:
        struct FileHeader {
            uint32_t fileId;
            uint32_t fileSize;
            char data[0];
        };

        File readFile(...) {
            read();
            decrypt();
        }

        File writeFile(...) {
            encrypt();
            write();
        }

    public:
        DatabaseFile();
        DatabaseFile(std::vector<File> files);

        static DatabaseFile read(std::istream) {
            while(!eof) {
                readFile();
            }
        }

        void write(std::ostream) {
            foreach(file) {
                writeFile();
            }
        }

        std::vector<File> files;
};

class DatabaseFileEncryption {
    // However you want it ...
};
strager