Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 1 | ############# |
| 2 | Encrypt Class |
| 3 | ############# |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 4 | |
Andrey Andreev | 6eb77da | 2014-05-31 21:18:17 +0300 | [diff] [blame] | 5 | The Encrypt Class provides two-way data encryption. It encrypted using |
| 6 | the Mcrypt PHP extension, which is required for the Encrypt Class to run. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 7 | |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 8 | .. important:: This library has been DEPRECATED and is only kept for |
| 9 | backwards compatibility. Please use the new :doc:`Encryption Library |
| 10 | <encryption>`. |
| 11 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 12 | .. contents:: |
| 13 | :local: |
| 14 | |
| 15 | .. raw:: html |
| 16 | |
| 17 | <div class="custom-index container"></div> |
| 18 | |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 19 | ************************* |
| 20 | Using the Encrypt Library |
| 21 | ************************* |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 22 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | Setting your Key |
| 24 | ================ |
| 25 | |
| 26 | A *key* is a piece of information that controls the cryptographic |
| 27 | process and permits an encrypted string to be decoded. In fact, the key |
| 28 | you chose will provide the **only** means to decode data that was |
| 29 | encrypted with that key, so not only must you choose the key carefully, |
| 30 | you must never change it if you intend use it for persistent data. |
| 31 | |
| 32 | It goes without saying that you should guard your key carefully. Should |
| 33 | someone gain access to your key, the data will be easily decoded. If |
| 34 | your server is not totally under your control it's impossible to ensure |
| 35 | key security so you may want to think carefully before using it for |
| 36 | anything that requires high security, like storing credit card numbers. |
| 37 | |
| 38 | To take maximum advantage of the encryption algorithm, your key should |
vlakoff | ac35e5a | 2012-06-15 22:59:26 +0300 | [diff] [blame] | 39 | 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] | 40 | string as you can concoct, with numbers and uppercase and lowercase |
| 41 | letters. Your key should **not** be a simple text string. In order to be |
| 42 | cryptographically secure it needs to be as random as possible. |
| 43 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 44 | 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] | 45 | you can design your own storage mechanism and pass the key dynamically |
| 46 | when encoding/decoding. |
| 47 | |
purwandi | 15cec71 | 2011-10-07 10:35:05 +0700 | [diff] [blame] | 48 | 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] | 49 | and set:: |
| 50 | |
| 51 | $config['encryption_key'] = "YOUR KEY"; |
| 52 | |
| 53 | Message Length |
| 54 | ============== |
| 55 | |
| 56 | It's important for you to know that the encoded messages the encryption |
| 57 | function generates will be approximately 2.6 times longer than the |
| 58 | original message. For example, if you encrypt the string "my super |
| 59 | secret data", which is 21 characters in length, you'll end up with an |
| 60 | encoded string that is roughly 55 characters (we say "roughly" because |
| 61 | the encoded string length increments in 64 bit clusters, so it's not |
| 62 | exactly linear). Keep this information in mind when selecting your data |
| 63 | storage mechanism. Cookies, for example, can only hold 4K of |
| 64 | information. |
| 65 | |
| 66 | Initializing the Class |
| 67 | ====================== |
| 68 | |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 69 | Like most other classes in CodeIgniter, the Encrypt class is |
| 70 | initialized in your controller using the ``$this->load->library()`` |
| 71 | method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 72 | |
| 73 | $this->load->library('encrypt'); |
| 74 | |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 75 | Once loaded, the Encrypt library object will be available using:: |
| 76 | |
| 77 | $this->encrypt |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 78 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 79 | *************** |
| 80 | Class Reference |
| 81 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 82 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 83 | .. php:class:: CI_Encrypt |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 85 | .. php:method:: encode($string[, $key = '']) |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 86 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 87 | :param string $string: Data to encrypt |
| 88 | :param string $key: Encryption key |
| 89 | :returns: Encrypted string |
| 90 | :rtype: string |
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 | Performs the data encryption and returns it as a string. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 94 | $msg = 'My secret message'; |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 95 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 96 | $encrypted_string = $this->encrypt->encode($msg); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 98 | You can optionally pass your encryption key via the second parameter if |
| 99 | you don't want to use the one in your config file:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 101 | $msg = 'My secret message'; |
| 102 | $key = 'super-secret-key'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 104 | $encrypted_string = $this->encrypt->encode($msg, $key); |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 106 | .. php:method:: decode($string[, $key = '']) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 108 | :param string $string: String to decrypt |
| 109 | :param string $key: Encryption key |
| 110 | :returns: Plain-text string |
| 111 | :rtype: string |
Derek Jones | 3e9fb1c | 2011-10-05 16:12:36 -0500 | [diff] [blame] | 112 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 113 | Decrypts an encoded string. Example:: |
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 | $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84'; |
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 | $plaintext_string = $this->encrypt->decode($encrypted_string); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 119 | You can optionally pass your encryption key via the second parameter if |
| 120 | you don't want to use the one in your config file:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 122 | $msg = 'My secret message'; |
| 123 | $key = 'super-secret-key'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 124 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 125 | $encrypted_string = $this->encrypt->decode($msg, $key); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 127 | .. php:method:: set_cipher($cipher) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 129 | :param int $cipher: Valid PHP MCrypt cypher constant |
| 130 | :returns: CI_Encrypt instance (method chaining) |
| 131 | :rtype: CI_Encrypt |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 133 | Permits you to set an Mcrypt cipher. By default it uses |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 134 | ``MCRYPT_RIJNDAEL_256``. Example:: |
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 | $this->encrypt->set_cipher(MCRYPT_BLOWFISH); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 137 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 138 | Please visit php.net for a list of `available ciphers <http://php.net/mcrypt>`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 140 | If you'd like to manually test whether your server supports MCrypt you |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 141 | can use:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 142 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 143 | echo extension_loaded('mcrypt') ? 'Yup' : 'Nope'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 145 | .. php:method:: set_mode($mode) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 147 | :param int $mode: Valid PHP MCrypt mode constant |
| 148 | :returns: CI_Encrypt instance (method chaining) |
| 149 | :rtype: CI_Encrypt |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 150 | |
| 151 | Permits you to set an Mcrypt mode. By default it uses **MCRYPT_MODE_CBC**. |
| 152 | Example:: |
| 153 | |
| 154 | $this->encrypt->set_mode(MCRYPT_MODE_CFB); |
| 155 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 156 | Please visit php.net for a list of `available modes <http://php.net/mcrypt>`_. |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 157 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 158 | .. php:method:: encode_from_legacy($string[, $legacy_mode = MCRYPT_MODE_ECB[, $key = '']]) |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 159 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 160 | :param string $string: String to encrypt |
| 161 | :param int $legacy_mode: Valid PHP MCrypt cipher constant |
| 162 | :param string $key: Encryption key |
| 163 | :returns: Newly encrypted string |
| 164 | :rtype: string |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 165 | |
| 166 | Enables you to re-encode data that was originally encrypted with |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 167 | CodeIgniter 1.x to be compatible with the Encrypt library in |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 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 |
Andrey Andreev | fc1e278 | 2014-02-09 17:36:24 +0200 | [diff] [blame] | 178 | Encrypt library have improved in CodeIgniter 2.x both for performance |
Derek Jones | 5d80d0f | 2013-07-24 17:44:29 -0700 | [diff] [blame] | 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 | ====================== =============== ======================================================================= |