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