Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Encryption Class |
| 3 | ################ |
| 4 | |
| 5 | The Encryption Class provides two-way data encryption. It uses a scheme |
| 6 | that either compiles the message using a randomly hashed bitwise XOR |
| 7 | encoding scheme, or is encrypted using the Mcrypt library. If Mcrypt is |
| 8 | not available on your server the encoded message will still provide a |
| 9 | reasonable degree of security for encrypted sessions or other such |
| 10 | "light" purposes. If Mcrypt is available, you'll be provided with a high |
| 11 | degree of security appropriate for storage. |
| 12 | |
| 13 | Setting your Key |
| 14 | ================ |
| 15 | |
| 16 | A *key* is a piece of information that controls the cryptographic |
| 17 | process and permits an encrypted string to be decoded. In fact, the key |
| 18 | you chose will provide the **only** means to decode data that was |
| 19 | encrypted with that key, so not only must you choose the key carefully, |
| 20 | you must never change it if you intend use it for persistent data. |
| 21 | |
| 22 | It goes without saying that you should guard your key carefully. Should |
| 23 | someone gain access to your key, the data will be easily decoded. If |
| 24 | your server is not totally under your control it's impossible to ensure |
| 25 | key security so you may want to think carefully before using it for |
| 26 | anything that requires high security, like storing credit card numbers. |
| 27 | |
| 28 | To take maximum advantage of the encryption algorithm, your key should |
vlakoff | ac35e5a | 2012-06-15 22:59:26 +0300 | [diff] [blame] | 29 | be 32 characters in length (256 bits). The key should be as random a |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | string as you can concoct, with numbers and uppercase and lowercase |
| 31 | letters. Your key should **not** be a simple text string. In order to be |
| 32 | cryptographically secure it needs to be as random as possible. |
| 33 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 34 | Your key can be either stored in your **application/config/config.php**, or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | you can design your own storage mechanism and pass the key dynamically |
| 36 | when encoding/decoding. |
| 37 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 38 | To save your key to your **application/config/config.php**, open the file |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | and set:: |
| 40 | |
| 41 | $config['encryption_key'] = "YOUR KEY"; |
| 42 | |
| 43 | Message Length |
| 44 | ============== |
| 45 | |
| 46 | It's important for you to know that the encoded messages the encryption |
| 47 | function generates will be approximately 2.6 times longer than the |
| 48 | original message. For example, if you encrypt the string "my super |
| 49 | secret data", which is 21 characters in length, you'll end up with an |
| 50 | encoded string that is roughly 55 characters (we say "roughly" because |
| 51 | the encoded string length increments in 64 bit clusters, so it's not |
| 52 | exactly linear). Keep this information in mind when selecting your data |
| 53 | storage mechanism. Cookies, for example, can only hold 4K of |
| 54 | information. |
| 55 | |
| 56 | Initializing the Class |
| 57 | ====================== |
| 58 | |
| 59 | Like most other classes in CodeIgniter, the Encryption class is |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 60 | initialized in your controller using the **$this->load->library** function:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 61 | |
| 62 | $this->load->library('encrypt'); |
| 63 | |
| 64 | Once loaded, the Encrypt library object will be available using: |
| 65 | $this->encrypt |
| 66 | |
| 67 | $this->encrypt->encode() |
| 68 | ======================== |
| 69 | |
| 70 | Performs the data encryption and returns it as a string. Example:: |
| 71 | |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 72 | $msg = 'My secret message'; |
| 73 | |
| 74 | $encrypted_string = $this->encrypt->encode($msg); |
| 75 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 76 | |
| 77 | You can optionally pass your encryption key via the second parameter if |
| 78 | you don't want to use the one in your config file:: |
| 79 | |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 80 | $msg = 'My secret message'; |
| 81 | $key = 'super-secret-key'; |
| 82 | |
| 83 | $encrypted_string = $this->encrypt->encode($msg, $key); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | $this->encrypt->decode() |
| 86 | ======================== |
| 87 | |
| 88 | Decrypts an encoded string. Example:: |
| 89 | |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 90 | $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84'; |
| 91 | |
| 92 | $plaintext_string = $this->encrypt->decode($encrypted_string); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
| 94 | You can optionally pass your encryption key via the second parameter if |
| 95 | you don't want to use the one in your config file:: |
| 96 | |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 97 | $msg = 'My secret message'; |
| 98 | $key = 'super-secret-key'; |
| 99 | |
| 100 | $encrypted_string = $this->encrypt->decode($msg, $key); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
| 102 | $this->encrypt->set_cipher(); |
| 103 | ============================== |
| 104 | |
| 105 | Permits you to set an Mcrypt cipher. By default it uses |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 106 | **MCRYPT_RIJNDAEL_256**. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
| 108 | $this->encrypt->set_cipher(MCRYPT_BLOWFISH); |
| 109 | |
| 110 | Please visit php.net for a list of `available |
| 111 | ciphers <http://php.net/mcrypt>`_. |
| 112 | |
| 113 | If you'd like to manually test whether your server supports Mcrypt you |
| 114 | can use:: |
| 115 | |
| 116 | echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup'; |
| 117 | |
| 118 | $this->encrypt->set_mode(); |
| 119 | ============================ |
| 120 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 121 | Permits you to set an Mcrypt mode. By default it uses **MCRYPT_MODE_CBC**. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | Example:: |
| 123 | |
| 124 | $this->encrypt->set_mode(MCRYPT_MODE_CFB); |
| 125 | |
| 126 | Please visit php.net for a list of `available |
| 127 | modes <http://php.net/mcrypt>`_. |
| 128 | |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 129 | $this->encrypt->encode_from_legacy($orig_data, $legacy_mode = MCRYPT_MODE_ECB, $key = ''); |
| 130 | ========================================================================================== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
| 132 | Enables you to re-encode data that was originally encrypted with |
| 133 | CodeIgniter 1.x to be compatible with the Encryption library in |
| 134 | CodeIgniter 2.x. It is only necessary to use this method if you have |
| 135 | encrypted data stored permanently such as in a file or database and are |
| 136 | on a server that supports Mcrypt. "Light" use encryption such as |
| 137 | encrypted session data or transitory encrypted flashdata require no |
| 138 | intervention on your part. However, existing encrypted Sessions will be |
| 139 | destroyed since data encrypted prior to 2.x will not be decoded. |
| 140 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 141 | .. important:: |
| 142 | **Why only a method to re-encode the data instead of maintaining legacy |
| 143 | methods for both encoding and decoding?** The algorithms in the |
| 144 | Encryption library have improved in CodeIgniter 2.x both for performance |
| 145 | and security, and we do not wish to encourage continued use of the older |
| 146 | methods. You can of course extend the Encryption library if you wish and |
| 147 | replace the new methods with the old and retain seamless compatibility |
| 148 | with CodeIgniter 1.x encrypted data, but this a decision that a |
| 149 | developer should make cautiously and deliberately, if at all. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 150 | |
| 151 | :: |
| 152 | |
| 153 | $new_data = $this->encrypt->encode_from_legacy($old_encrypted_string); |
| 154 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 155 | ====================== =============== ======================================================================= |
| 156 | Parameter Default Description |
| 157 | ====================== =============== ======================================================================= |
| 158 | **$orig_data** n/a The original encrypted data from CodeIgniter 1.x's Encryption library |
| 159 | **$legacy_mode** MCRYPT_MODE_ECB The Mcrypt mode that was used to generate the original encrypted data. |
| 160 | CodeIgniter 1.x's default was MCRYPT_MODE_ECB, and it will assume that |
| 161 | to be the case unless overridden by this parameter. |
| 162 | **$key** n/a The encryption key. This it typically specified in your config file as |
| 163 | outlined above. |
| 164 | ====================== =============== ======================================================================= |