CodeIgniter Encrypt Class: Watch For Corruption!

I had a special problem this week, and it took some digging to find out what was going wrong. It turns out that an encrypted cookie I was setting was being corrupted by a different encryption class configuration.

In a project I am working on, we are using the encryption class a lot, and in many cases providing a special cipher and/or key configuration. Since we are doing that, it’s important to remember what cipher and key are being used and when. When working on a large application, it can be difficult to remember what is being encrypted, and so I had to work out a solution.

  1. Save the current config
  2. Set the desired config for this encryption/decryption
  3. Restore the previous config

I ended up extending the Encrypt class with simple methods to take care of tasks 1 and 3. This works nicely, and no more corrupted cookie data.

This problem makes me wonder if the Encrypt class should not be limited to a singleton, but the very nature of CodeIgniter libraries is that they are.