Leggy7
Leggy7

Reputation: 493

best practices to compress/encrypt

My problem is related on how store massive informations on a 3D terrain. These informations should be secret and since they are very bulky should also be compressed. I opted for file storage and now I was wondering to know the best pracitces to encrypt/compress (or compress/encrypt) object data to files.

I just don't even know if it would be better to write to file and then compress and encrypt it or if work on a data stream and then write to file.

Any suggestion will be appreciated!

Upvotes: 5

Views: 3247

Answers (2)

Bradley Smith
Bradley Smith

Reputation: 119

Compression and encryption are interesting beasts - the act of encryption randomises the bits, which then makes GZip compression more difficult.

See the notes in this file. https://serverfault.com/questions/17855/can-i-compress-an-encrypted-file

Doing as CodeCaster says in that order will produce the smallest file, as the file is already compressed before encyrption.

Also consider file size and the types of disks that store it, along with the types of machines that decrypt and uncompress. If you are streaming it back into memory, you may find you run out of memory in certain situations, unless you take the stream up into buffers, or break the files up.

Upvotes: 4

CodeCaster
CodeCaster

Reputation: 151588

I just don't even know if it would be better to write to file and then compress and encrypt it or if work on a data stream and then write to file.

"Better" is not measurable. In-memory compression and encryption may be faster than directly writing to file, but can easily prove troublesome if the data is larger than what fits in memory.

As you do want the end result stored on disk, I'd approach it like this (first compress, then encrypt, then write to disk):

using (var compressionStream = new CompressionStream(rawData))
{
    using (var encryptionStream = new EncryptionStream(compressionStream))
    {
        using (var fileStream = new FileStream("outputfile"))
        {
            encryptionStream.CopyTo(fileStream);
        }
    }
}

The implementations of CompressionStream and EncryptionStream of course depend on the APIs you use.

Upvotes: 7

Related Questions