blob: 67e2a01905cbfdbf74a7be61efa373ae3e728d94 [file] [log] [blame]
Andrey Andreevfc1e2782014-02-09 17:36:24 +02001#############
2Encrypt Class
3#############
Derek Jones8ede1a22011-10-05 13:34:52 -05004
Andrey Andreev6eb77da2014-05-31 21:18:17 +03005The Encrypt Class provides two-way data encryption. It encrypted using
6the Mcrypt PHP extension, which is required for the Encrypt Class to run.
Derek Jones8ede1a22011-10-05 13:34:52 -05007
Andrey Andreevfc1e2782014-02-09 17:36:24 +02008.. 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 Jones5d80d0f2013-07-24 17:44:29 -070012.. contents::
13 :local:
14
15.. raw:: html
16
17 <div class="custom-index container"></div>
18
Andrey Andreevfc1e2782014-02-09 17:36:24 +020019*************************
20Using the Encrypt Library
21*************************
Derek Jones5d80d0f2013-07-24 17:44:29 -070022
Derek Jones8ede1a22011-10-05 13:34:52 -050023Setting your Key
24================
25
26A *key* is a piece of information that controls the cryptographic
27process and permits an encrypted string to be decoded. In fact, the key
28you chose will provide the **only** means to decode data that was
29encrypted with that key, so not only must you choose the key carefully,
30you must never change it if you intend use it for persistent data.
31
32It goes without saying that you should guard your key carefully. Should
33someone gain access to your key, the data will be easily decoded. If
34your server is not totally under your control it's impossible to ensure
35key security so you may want to think carefully before using it for
36anything that requires high security, like storing credit card numbers.
37
38To take maximum advantage of the encryption algorithm, your key should
vlakoffac35e5a2012-06-15 22:59:26 +030039be 32 characters in length (256 bits). The key should be as random a
Derek Jones8ede1a22011-10-05 13:34:52 -050040string as you can concoct, with numbers and uppercase and lowercase
41letters. Your key should **not** be a simple text string. In order to be
42cryptographically secure it needs to be as random as possible.
43
purwandi15cec712011-10-07 10:35:05 +070044Your key can be either stored in your **application/config/config.php**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050045you can design your own storage mechanism and pass the key dynamically
46when encoding/decoding.
47
purwandi15cec712011-10-07 10:35:05 +070048To save your key to your **application/config/config.php**, open the file
Derek Jones8ede1a22011-10-05 13:34:52 -050049and set::
50
51 $config['encryption_key'] = "YOUR KEY";
52
53Message Length
54==============
55
56It's important for you to know that the encoded messages the encryption
57function generates will be approximately 2.6 times longer than the
58original message. For example, if you encrypt the string "my super
59secret data", which is 21 characters in length, you'll end up with an
60encoded string that is roughly 55 characters (we say "roughly" because
61the encoded string length increments in 64 bit clusters, so it's not
62exactly linear). Keep this information in mind when selecting your data
63storage mechanism. Cookies, for example, can only hold 4K of
64information.
65
66Initializing the Class
67======================
68
Andrey Andreevfc1e2782014-02-09 17:36:24 +020069Like most other classes in CodeIgniter, the Encrypt class is
70initialized in your controller using the ``$this->load->library()``
71method::
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73 $this->load->library('encrypt');
74
Andrey Andreevfc1e2782014-02-09 17:36:24 +020075Once loaded, the Encrypt library object will be available using::
76
77 $this->encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -050078
Derek Jones5d80d0f2013-07-24 17:44:29 -070079***************
80Class Reference
81***************
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020083.. php:class:: CI_Encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -050084
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020085 .. php:method:: encode($string[, $key = ''])
Derek Jones3e9fb1c2011-10-05 16:12:36 -050086
Andrey Andreev28c2c972014-02-08 04:27:48 +020087 :param string $string: Data to encrypt
88 :param string $key: Encryption key
89 :returns: Encrypted string
90 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones5d80d0f2013-07-24 17:44:29 -070092 Performs the data encryption and returns it as a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Derek Jones5d80d0f2013-07-24 17:44:29 -070094 $msg = 'My secret message';
Derek Jones3e9fb1c2011-10-05 16:12:36 -050095
Derek Jones5d80d0f2013-07-24 17:44:29 -070096 $encrypted_string = $this->encrypt->encode($msg);
Derek Jones8ede1a22011-10-05 13:34:52 -050097
Derek Jones5d80d0f2013-07-24 17:44:29 -070098 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 Jones8ede1a22011-10-05 13:34:52 -0500100
Derek Jones5d80d0f2013-07-24 17:44:29 -0700101 $msg = 'My secret message';
102 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Derek Jones5d80d0f2013-07-24 17:44:29 -0700104 $encrypted_string = $this->encrypt->encode($msg, $key);
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500105
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200106 .. php:method:: decode($string[, $key = ''])
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreev28c2c972014-02-08 04:27:48 +0200108 :param string $string: String to decrypt
109 :param string $key: Encryption key
110 :returns: Plain-text string
111 :rtype: string
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500112
Derek Jones5d80d0f2013-07-24 17:44:29 -0700113 Decrypts an encoded string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jones5d80d0f2013-07-24 17:44:29 -0700115 $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jones5d80d0f2013-07-24 17:44:29 -0700117 $plaintext_string = $this->encrypt->decode($encrypted_string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jones5d80d0f2013-07-24 17:44:29 -0700119 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 Jones8ede1a22011-10-05 13:34:52 -0500121
Derek Jones5d80d0f2013-07-24 17:44:29 -0700122 $msg = 'My secret message';
123 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jones5d80d0f2013-07-24 17:44:29 -0700125 $encrypted_string = $this->encrypt->decode($msg, $key);
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200127 .. php:method:: set_cipher($cipher)
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
Andrey Andreev28c2c972014-02-08 04:27:48 +0200129 :param int $cipher: Valid PHP MCrypt cypher constant
130 :returns: CI_Encrypt instance (method chaining)
131 :rtype: CI_Encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Derek Jones5d80d0f2013-07-24 17:44:29 -0700133 Permits you to set an Mcrypt cipher. By default it uses
Andrey Andreev28c2c972014-02-08 04:27:48 +0200134 ``MCRYPT_RIJNDAEL_256``. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Derek Jones5d80d0f2013-07-24 17:44:29 -0700136 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
Derek Jones8ede1a22011-10-05 13:34:52 -0500137
Andrey Andreev28c2c972014-02-08 04:27:48 +0200138 Please visit php.net for a list of `available ciphers <http://php.net/mcrypt>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreev28c2c972014-02-08 04:27:48 +0200140 If you'd like to manually test whether your server supports MCrypt you
Derek Jones5d80d0f2013-07-24 17:44:29 -0700141 can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
Andrey Andreev28c2c972014-02-08 04:27:48 +0200143 echo extension_loaded('mcrypt') ? 'Yup' : 'Nope';
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200145 .. php:method:: set_mode($mode)
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreev28c2c972014-02-08 04:27:48 +0200147 :param int $mode: Valid PHP MCrypt mode constant
148 :returns: CI_Encrypt instance (method chaining)
149 :rtype: CI_Encrypt
Derek Jones5d80d0f2013-07-24 17:44:29 -0700150
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 Andreev28c2c972014-02-08 04:27:48 +0200156 Please visit php.net for a list of `available modes <http://php.net/mcrypt>`_.
Derek Jones5d80d0f2013-07-24 17:44:29 -0700157
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200158 .. php:method:: encode_from_legacy($string[, $legacy_mode = MCRYPT_MODE_ECB[, $key = '']])
Derek Jones5d80d0f2013-07-24 17:44:29 -0700159
Andrey Andreev28c2c972014-02-08 04:27:48 +0200160 :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 Jones5d80d0f2013-07-24 17:44:29 -0700165
166 Enables you to re-encode data that was originally encrypted with
Andrey Andreevfc1e2782014-02-09 17:36:24 +0200167 CodeIgniter 1.x to be compatible with the Encrypt library in
Derek Jones5d80d0f2013-07-24 17:44:29 -0700168 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 Andreevfc1e2782014-02-09 17:36:24 +0200178 Encrypt library have improved in CodeIgniter 2.x both for performance
Derek Jones5d80d0f2013-07-24 17:44:29 -0700179 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 ====================== =============== =======================================================================