blob: f7235bfd20686ba32f1d4aaf4a21bdc8b33e6ec7 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Encryption Class
3################
4
5The Encryption Class provides two-way data encryption. It uses a scheme
6that 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
Derek Jones5d80d0f2013-07-24 17:44:29 -070013.. contents::
14 :local:
15
16.. raw:: html
17
18 <div class="custom-index container"></div>
19
20****************************
21Using the Encryption Library
22****************************
23
Derek Jones8ede1a22011-10-05 13:34:52 -050024Setting your Key
25================
26
27A *key* is a piece of information that controls the cryptographic
28process and permits an encrypted string to be decoded. In fact, the key
29you chose will provide the **only** means to decode data that was
30encrypted with that key, so not only must you choose the key carefully,
31you must never change it if you intend use it for persistent data.
32
33It goes without saying that you should guard your key carefully. Should
34someone gain access to your key, the data will be easily decoded. If
35your server is not totally under your control it's impossible to ensure
36key security so you may want to think carefully before using it for
37anything that requires high security, like storing credit card numbers.
38
39To take maximum advantage of the encryption algorithm, your key should
vlakoffac35e5a2012-06-15 22:59:26 +030040be 32 characters in length (256 bits). The key should be as random a
Derek Jones8ede1a22011-10-05 13:34:52 -050041string as you can concoct, with numbers and uppercase and lowercase
42letters. Your key should **not** be a simple text string. In order to be
43cryptographically secure it needs to be as random as possible.
44
purwandi15cec712011-10-07 10:35:05 +070045Your key can be either stored in your **application/config/config.php**, or
Derek Jones8ede1a22011-10-05 13:34:52 -050046you can design your own storage mechanism and pass the key dynamically
47when encoding/decoding.
48
purwandi15cec712011-10-07 10:35:05 +070049To save your key to your **application/config/config.php**, open the file
Derek Jones8ede1a22011-10-05 13:34:52 -050050and set::
51
52 $config['encryption_key'] = "YOUR KEY";
53
54Message Length
55==============
56
57It's important for you to know that the encoded messages the encryption
58function generates will be approximately 2.6 times longer than the
59original message. For example, if you encrypt the string "my super
60secret data", which is 21 characters in length, you'll end up with an
61encoded string that is roughly 55 characters (we say "roughly" because
62the encoded string length increments in 64 bit clusters, so it's not
63exactly linear). Keep this information in mind when selecting your data
64storage mechanism. Cookies, for example, can only hold 4K of
65information.
66
67Initializing the Class
68======================
69
70Like most other classes in CodeIgniter, the Encryption class is
purwandi15cec712011-10-07 10:35:05 +070071initialized in your controller using the **$this->load->library** function::
Derek Jones8ede1a22011-10-05 13:34:52 -050072
73 $this->load->library('encrypt');
74
Derek Jones5d80d0f2013-07-24 17:44:29 -070075Once loaded, the Encrypt library object will be available using
76``$this->encrypt``
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Derek Jones5d80d0f2013-07-24 17:44:29 -070078***************
79Class Reference
80***************
Derek Jones8ede1a22011-10-05 13:34:52 -050081
Derek Jones5d80d0f2013-07-24 17:44:29 -070082.. class:: CI_Encrypt
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jones5d80d0f2013-07-24 17:44:29 -070084 .. method:: encode($string, $key = '')
Derek Jones3e9fb1c2011-10-05 16:12:36 -050085
Derek Jones5d80d0f2013-07-24 17:44:29 -070086 :param string $string: contents to be encrypted
87 :param string $key: encryption key
88 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050089
Derek Jones5d80d0f2013-07-24 17:44:29 -070090 Performs the data encryption and returns it as a string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Derek Jones5d80d0f2013-07-24 17:44:29 -070092 $msg = 'My secret message';
Derek Jones3e9fb1c2011-10-05 16:12:36 -050093
Derek Jones5d80d0f2013-07-24 17:44:29 -070094 $encrypted_string = $this->encrypt->encode($msg);
Derek Jones8ede1a22011-10-05 13:34:52 -050095
Derek Jones5d80d0f2013-07-24 17:44:29 -070096 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 Jones8ede1a22011-10-05 13:34:52 -050098
Derek Jones5d80d0f2013-07-24 17:44:29 -070099 $msg = 'My secret message';
100 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
Derek Jones5d80d0f2013-07-24 17:44:29 -0700102 $encrypted_string = $this->encrypt->encode($msg, $key);
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500103
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Derek Jones5d80d0f2013-07-24 17:44:29 -0700105 .. method:: decode($string, $key = '')
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Derek Jones5d80d0f2013-07-24 17:44:29 -0700107 :param string $string: contents to be decrypted
108 :param string $key: encryption key
109 :returns: string
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500110
Derek Jones5d80d0f2013-07-24 17:44:29 -0700111 Decrypts an encoded string. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500112
Derek Jones5d80d0f2013-07-24 17:44:29 -0700113 $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Derek Jones5d80d0f2013-07-24 17:44:29 -0700115 $plaintext_string = $this->encrypt->decode($encrypted_string);
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Derek Jones5d80d0f2013-07-24 17:44:29 -0700117 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 Jones8ede1a22011-10-05 13:34:52 -0500119
Derek Jones5d80d0f2013-07-24 17:44:29 -0700120 $msg = 'My secret message';
121 $key = 'super-secret-key';
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Derek Jones5d80d0f2013-07-24 17:44:29 -0700123 $encrypted_string = $this->encrypt->decode($msg, $key);
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Derek Jones5d80d0f2013-07-24 17:44:29 -0700126 .. method:: set_cipher($cipher)
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Derek Jones5d80d0f2013-07-24 17:44:29 -0700128 :param int $cipher: valid PHP Mcrypt cypher constant
129 :returns: CI_Encrypt object for method chaining
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Derek Jones5d80d0f2013-07-24 17:44:29 -0700131 Permits you to set an Mcrypt cipher. By default it uses
132 **MCRYPT_RIJNDAEL_256**. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
Derek Jones5d80d0f2013-07-24 17:44:29 -0700134 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
Derek Jones5d80d0f2013-07-24 17:44:29 -0700136 Please visit php.net for a list of `available
137 ciphers <http://php.net/mcrypt>`_.
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Derek Jones5d80d0f2013-07-24 17:44:29 -0700139 If you'd like to manually test whether your server supports Mcrypt you
140 can use::
Derek Jones8ede1a22011-10-05 13:34:52 -0500141
Derek Jones5d80d0f2013-07-24 17:44:29 -0700142 echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Derek Jones8ede1a22011-10-05 13:34:52 -0500144
Derek Jones5d80d0f2013-07-24 17:44:29 -0700145 .. method:: set_mode($mode)
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Derek Jones5d80d0f2013-07-24 17:44:29 -0700147 :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 ====================== =============== =======================================================================