blob: faff39975e05807775fbd40d6063c3fd8f88615f [file] [log] [blame]
Andrey Andreevfc1e2782014-02-09 17:36:24 +02001#############
2Encrypt Class
3#############
Derek Jones8ede1a22011-10-05 13:34:52 -05004
Andrey Andreevfc1e2782014-02-09 17:36:24 +02005The Encrypt Class provides two-way data encryption. It uses a scheme
Derek Jones8ede1a22011-10-05 13:34:52 -05006that either compiles the message using a randomly hashed bitwise XOR
7encoding scheme, or is encrypted using the Mcrypt library. If Mcrypt is
8not available on your server the encoded message will still provide a
9reasonable degree of security for encrypted sessions or other such
10"light" purposes. If Mcrypt is available, you'll be provided with a high
11degree of security appropriate for storage.
12
Andrey Andreevfc1e2782014-02-09 17:36:24 +020013.. 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 Jones5d80d0f2013-07-24 17:44:29 -070017.. contents::
18 :local:
19
20.. raw:: html
21
22 <div class="custom-index container"></div>
23
Andrey Andreevfc1e2782014-02-09 17:36:24 +020024*************************
25Using the Encrypt Library
26*************************
Derek Jones5d80d0f2013-07-24 17:44:29 -070027
Derek Jones8ede1a22011-10-05 13:34:52 -050028Setting your Key
29================
30
31A *key* is a piece of information that controls the cryptographic
32process and permits an encrypted string to be decoded. In fact, the key
33you chose will provide the **only** means to decode data that was
34encrypted with that key, so not only must you choose the key carefully,
35you must never change it if you intend use it for persistent data.
36
37It goes without saying that you should guard your key carefully. Should
38someone gain access to your key, the data will be easily decoded. If
39your server is not totally under your control it's impossible to ensure
40key security so you may want to think carefully before using it for
41anything that requires high security, like storing credit card numbers.
42
43To take maximum advantage of the encryption algorithm, your key should
vlakoffac35e5a2012-06-15 22:59:26 +030044be 32 characters in length (256 bits). The key should be as random a
Derek Jones8ede1a22011-10-05 13:34:52 -050045string as you can concoct, with numbers and uppercase and lowercase
46letters. Your key should **not** be a simple text string. In order to be
47cryptographically secure it needs to be as random as possible.
48
purwandi15cec712011-10-07 10:35:05 +070049Your key can be either stored in your **application/config/config.php**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050050you can design your own storage mechanism and pass the key dynamically
51when encoding/decoding.
52
purwandi15cec712011-10-07 10:35:05 +070053To save your key to your **application/config/config.php**, open the file
Derek Jones8ede1a22011-10-05 13:34:52 -050054and set::
55
56 $config['encryption_key'] = "YOUR KEY";
57
58Message Length
59==============
60
61It's important for you to know that the encoded messages the encryption
62function generates will be approximately 2.6 times longer than the
63original message. For example, if you encrypt the string "my super
64secret data", which is 21 characters in length, you'll end up with an
65encoded string that is roughly 55 characters (we say "roughly" because
66the encoded string length increments in 64 bit clusters, so it's not
67exactly linear). Keep this information in mind when selecting your data
68storage mechanism. Cookies, for example, can only hold 4K of
69information.
70
71Initializing the Class
72======================
73
Andrey Andreevfc1e2782014-02-09 17:36:24 +020074Like most other classes in CodeIgniter, the Encrypt class is
75initialized in your controller using the ``$this->load->library()``
76method::
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78 $this->load->library('encrypt');
79
Andrey Andreevfc1e2782014-02-09 17:36:24 +020080Once loaded, the Encrypt library object will be available using::
81
82 $this->encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jones5d80d0f2013-07-24 17:44:29 -070084***************
85Class Reference
86***************
Derek Jones8ede1a22011-10-05 13:34:52 -050087
Derek Jones5d80d0f2013-07-24 17:44:29 -070088.. class:: CI_Encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Andrey Andreev28c2c972014-02-08 04:27:48 +020090 .. method:: encode($string[, $key = ''])
Derek Jones3e9fb1c2011-10-05 16:12:36 -050091
Andrey Andreev28c2c972014-02-08 04:27:48 +020092 :param string $string: Data to encrypt
93 :param string $key: Encryption key
94 :returns: Encrypted string
95 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Derek Jones5d80d0f2013-07-24 17:44:29 -070097 Performs the data encryption and returns it as a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050098
Derek Jones5d80d0f2013-07-24 17:44:29 -070099 $msg = 'My secret message';
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500100
Derek Jones5d80d0f2013-07-24 17:44:29 -0700101 $encrypted_string = $this->encrypt->encode($msg);
Derek Jones8ede1a22011-10-05 13:34:52 -0500102
Derek Jones5d80d0f2013-07-24 17:44:29 -0700103 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 Jones8ede1a22011-10-05 13:34:52 -0500105
Derek Jones5d80d0f2013-07-24 17:44:29 -0700106 $msg = 'My secret message';
107 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jones5d80d0f2013-07-24 17:44:29 -0700109 $encrypted_string = $this->encrypt->encode($msg, $key);
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500110
Andrey Andreev28c2c972014-02-08 04:27:48 +0200111 .. method:: decode($string[, $key = ''])
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Andrey Andreev28c2c972014-02-08 04:27:48 +0200113 :param string $string: String to decrypt
114 :param string $key: Encryption key
115 :returns: Plain-text string
116 :rtype: string
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500117
Derek Jones5d80d0f2013-07-24 17:44:29 -0700118 Decrypts an encoded string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Derek Jones5d80d0f2013-07-24 17:44:29 -0700120 $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Derek Jones5d80d0f2013-07-24 17:44:29 -0700122 $plaintext_string = $this->encrypt->decode($encrypted_string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
Derek Jones5d80d0f2013-07-24 17:44:29 -0700124 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 Jones8ede1a22011-10-05 13:34:52 -0500126
Derek Jones5d80d0f2013-07-24 17:44:29 -0700127 $msg = 'My secret message';
128 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500129
Derek Jones5d80d0f2013-07-24 17:44:29 -0700130 $encrypted_string = $this->encrypt->decode($msg, $key);
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Derek Jones5d80d0f2013-07-24 17:44:29 -0700132 .. method:: set_cipher($cipher)
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Andrey Andreev28c2c972014-02-08 04:27:48 +0200134 :param int $cipher: Valid PHP MCrypt cypher constant
135 :returns: CI_Encrypt instance (method chaining)
136 :rtype: CI_Encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
Derek Jones5d80d0f2013-07-24 17:44:29 -0700138 Permits you to set an Mcrypt cipher. By default it uses
Andrey Andreev28c2c972014-02-08 04:27:48 +0200139 ``MCRYPT_RIJNDAEL_256``. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500140
Derek Jones5d80d0f2013-07-24 17:44:29 -0700141 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev28c2c972014-02-08 04:27:48 +0200143 Please visit php.net for a list of `available ciphers <http://php.net/mcrypt>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreev28c2c972014-02-08 04:27:48 +0200145 If you'd like to manually test whether your server supports MCrypt you
Derek Jones5d80d0f2013-07-24 17:44:29 -0700146 can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
Andrey Andreev28c2c972014-02-08 04:27:48 +0200148 echo extension_loaded('mcrypt') ? 'Yup' : 'Nope';
Derek Jones8ede1a22011-10-05 13:34:52 -0500149
Derek Jones5d80d0f2013-07-24 17:44:29 -0700150 .. method:: set_mode($mode)
Derek Jones8ede1a22011-10-05 13:34:52 -0500151
Andrey Andreev28c2c972014-02-08 04:27:48 +0200152 :param int $mode: Valid PHP MCrypt mode constant
153 :returns: CI_Encrypt instance (method chaining)
154 :rtype: CI_Encrypt
Derek Jones5d80d0f2013-07-24 17:44:29 -0700155
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 Andreev28c2c972014-02-08 04:27:48 +0200161 Please visit php.net for a list of `available modes <http://php.net/mcrypt>`_.
Derek Jones5d80d0f2013-07-24 17:44:29 -0700162
163 .. method:: encode_from_legacy($string[, $legacy_mode = MCRYPT_MODE_ECB[, $key = '']])
164
Andrey Andreev28c2c972014-02-08 04:27:48 +0200165 :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 Jones5d80d0f2013-07-24 17:44:29 -0700170
171 Enables you to re-encode data that was originally encrypted with
Andrey Andreevfc1e2782014-02-09 17:36:24 +0200172 CodeIgniter 1.x to be compatible with the Encrypt library in
Derek Jones5d80d0f2013-07-24 17:44:29 -0700173 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 Andreevfc1e2782014-02-09 17:36:24 +0200183 Encrypt library have improved in CodeIgniter 2.x both for performance
Derek Jones5d80d0f2013-07-24 17:44:29 -0700184 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 ====================== =============== =======================================================================