Encryption/Decryption of Any File type

Hey everyone-

I am trying to encode a file (say for instance, a wav file) so that once encrypted it cannot be opened or modified until I have decrypted it again back to it’s original form. I’ve been poking around and it seems the best way to do this is with the Blowfish class, by turning the file into a MemoryBlock.

-Is this the best way of going about this? If not is there a better way?
-If so, how do I go about using the Blowfish class to encrypt a MemoryBlock? I guess I need to do some conversion to a 32-bit integer and I need to iterate through the memory block 2 integers at the time?

Any tips or prodding in the right direction would be great, I’m not too sure where to begin. I’ve been able to encrypt text files using strings and Blowfish but don’t know if it is possible to do something similar for an entire file type.


Not sure if this is the right way to do it but whenever I’ve needed to encrypt data I’ve always used the RASKey class. With that it’s quite easy to load your data into a MemoryBlock, convert it to a BigInteger (using BigInteger::loadFromMemoryBlock) and then encrypt/decrypt it RSAKey::applyToValue.

I’ve only really used it for smallish things like passwords or XML files when sending over the net etc. but I can’t see why it would’t work with arbitrarily sized data. Haven’t really looked into the speed of it either and if it scales linearly with size or not. I guess the main advantage to using RSA is the asymmetric key pairing but I don’t know if you need that.

It would be interesting to know what the ‘correct’ usages of each of the classes is though.

Hm, might be worth looking into. I’ve tried doing RSAkey encryption with text before and it seems like it slows down the larger the data is, where as Blowfish has been much faster for me (again this is still only using text). I suppose I’ll try using Blowfish by converting the file to a MemoryBlock and then to a BigInteger.

Has anyone else happened to use Blowfish for encrypting things other than text before?

RSA is asymmetric while Blowfish is symmetric. If you don’t want to ship your encryption key with your program (e.g. license files) you need to use RSA. Your description sounds like encrypting/decrypting is done by your program. In this case Blowfish is a lot faster than RSA.
Blowfish only works with 64-bit boundaries, so you need to store the original file size somewhere. You can encrypt a MemoryBlock by casting to uint32:

MemoryBlock mb;
uint32* data = (uint32*) mb.getData();

Again, be aware that you need to add some logic to avoid reading beyond the MemoryBlock’s data if its size doesn’t fit exactly in an even number of uint32s.

Thanks ckk, that’s very helpful! When you say I need to store the original file size somewhere, do you mean the size of the file itself or the size of the memory block?

The file size. If you have a 15 byte sized file you’ll get 16 bytes of encrypted data (4x uint32). When decrypting you probably want the original 15 byte so you need to remember that the file is actually 15 bytes (or that you padded one byte).

I got it to work, thanks for your help! :smiley: