# Encryption/Decryption of Any File type

**URL:** <https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800>\
**Category:** General JUCE discussion\
**Created:** [July 16, 2013, 8:20pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800 "2013-07-16T20:20:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlongabaugh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tlongabaugh/32/18640_2.png) [@tlongabaugh](https://forum.juce.com/u/tlongabaugh)\
**Post date:** [July 16, 2013, 8:20pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/1 "2013-07-16T20:20:23Z")

</div>

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.

-Tom

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [July 16, 2013, 9:31pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/2 "2013-07-16T21:31:59Z")

</div>

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](http://rawmaterialsoftware.com/api/classBigInteger.html#af1ad0e8dcaaa34c7d14d01dca224a668)) and then encrypt/decrypt it [RSAKey::applyToValue](http://rawmaterialsoftware.com/api/classRSAKey.html#a66512b0a53fad1f68481b4aca050f93c).

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.

---

<div class="post-metadata">

**Author:** ![tlongabaugh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tlongabaugh/32/18640_2.png) [@tlongabaugh](https://forum.juce.com/u/tlongabaugh)\
**Post date:** [July 18, 2013, 1:38pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/3 "2013-07-18T13:38:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [July 19, 2013, 5:34am UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/4 "2013-07-19T05:34:16Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![tlongabaugh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tlongabaugh/32/18640_2.png) [@tlongabaugh](https://forum.juce.com/u/tlongabaugh)\
**Post date:** [July 22, 2013, 4:21pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/5 "2013-07-22T16:21:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [July 23, 2013, 5:58am UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/6 "2013-07-23T05:58:44Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![tlongabaugh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tlongabaugh/32/18640_2.png) [@tlongabaugh](https://forum.juce.com/u/tlongabaugh)\
**Post date:** [July 25, 2013, 4:13pm UTC](https://forum.juce.com/t/encryption-decryption-of-any-file-type/10800/7 "2013-07-25T16:13:40Z")

</div>

I got it to work, thanks for your help! 😃
