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