blob: 356465b21622b907f3056aad033b119b7a069e68 [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
13Setting your Key
14================
15
16A *key* is a piece of information that controls the cryptographic
17process and permits an encrypted string to be decoded. In fact, the key
18you chose will provide the **only** means to decode data that was
19encrypted with that key, so not only must you choose the key carefully,
20you must never change it if you intend use it for persistent data.
21
22It goes without saying that you should guard your key carefully. Should
23someone gain access to your key, the data will be easily decoded. If
24your server is not totally under your control it's impossible to ensure
25key security so you may want to think carefully before using it for
26anything that requires high security, like storing credit card numbers.
27
28To take maximum advantage of the encryption algorithm, your key should
29be 32 characters in length (128 bits). The key should be as random a
30string as you can concoct, with numbers and uppercase and lowercase
31letters. Your key should **not** be a simple text string. In order to be
32cryptographically secure it needs to be as random as possible.
33
34Your key can be either stored in your application/config/config.php, or
35you can design your own storage mechanism and pass the key dynamically
36when encoding/decoding.
37
38To save your key to your application/config/config.php, open the file
39and set::
40
41 $config['encryption_key'] = "YOUR KEY";
42
43Message Length
44==============
45
46It's important for you to know that the encoded messages the encryption
47function generates will be approximately 2.6 times longer than the
48original message. For example, if you encrypt the string "my super
49secret data", which is 21 characters in length, you'll end up with an
50encoded string that is roughly 55 characters (we say "roughly" because
51the encoded string length increments in 64 bit clusters, so it's not
52exactly linear). Keep this information in mind when selecting your data
53storage mechanism. Cookies, for example, can only hold 4K of
54information.
55
56Initializing the Class
57======================
58
59Like most other classes in CodeIgniter, the Encryption class is
60initialized in your controller using the $this->load->library function::
61
62 $this->load->library('encrypt');
63
64Once loaded, the Encrypt library object will be available using:
65$this->encrypt
66
67$this->encrypt->encode()
68========================
69
70Performs the data encryption and returns it as a string. Example::
71
Derek Jones3e9fb1c2011-10-05 16:12:36 -050072 $msg = 'My secret message';
73
74 $encrypted_string = $this->encrypt->encode($msg);
75
Derek Jones8ede1a22011-10-05 13:34:52 -050076
77You can optionally pass your encryption key via the second parameter if
78you don't want to use the one in your config file::
79
Derek Jones3e9fb1c2011-10-05 16:12:36 -050080 $msg = 'My secret message';
81 $key = 'super-secret-key';
82
83 $encrypted_string = $this->encrypt->encode($msg, $key);
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85$this->encrypt->decode()
86========================
87
88Decrypts an encoded string. Example::
89
Derek Jones3e9fb1c2011-10-05 16:12:36 -050090 $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
91
92 $plaintext_string = $this->encrypt->decode($encrypted_string);
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94You can optionally pass your encryption key via the second parameter if
95you don't want to use the one in your config file::
96
Derek Jones3e9fb1c2011-10-05 16:12:36 -050097 $msg = 'My secret message';
98 $key = 'super-secret-key';
99
100 $encrypted_string = $this->encrypt->decode($msg, $key);
Derek Jones8ede1a22011-10-05 13:34:52 -0500101
102$this->encrypt->set_cipher();
103==============================
104
105Permits you to set an Mcrypt cipher. By default it uses
106MCRYPT_RIJNDAEL_256. Example::
107
108 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
109
110Please visit php.net for a list of `available
111ciphers <http://php.net/mcrypt>`_.
112
113If you'd like to manually test whether your server supports Mcrypt you
114can use::
115
116 echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';
117
118$this->encrypt->set_mode();
119============================
120
121Permits you to set an Mcrypt mode. By default it uses MCRYPT_MODE_CBC.
122Example::
123
124 $this->encrypt->set_mode(MCRYPT_MODE_CFB);
125
126Please visit php.net for a list of `available
127modes <http://php.net/mcrypt>`_.
128
129$this->encrypt->sha1();
130=======================
131
132SHA1 encoding function. Provide a string and it will return a 160 bit
133one way hash. Note: SHA1, just like MD5 is non-decodable. Example::
134
135 $hash = $this->encrypt->sha1('Some string');
136
137Many PHP installations have SHA1 support by default so if all you need
138is to encode a hash it's simpler to use the native function::
139
140 $hash = sha1('Some string');
141
142If your server does not support SHA1 you can use the provided function.
143
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500144$this->encrypt->encode_from_legacy($orig_data, $legacy_mode = MCRYPT_MODE_ECB, $key = '');
145==========================================================================================
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
147Enables you to re-encode data that was originally encrypted with
148CodeIgniter 1.x to be compatible with the Encryption library in
149CodeIgniter 2.x. It is only necessary to use this method if you have
150encrypted data stored permanently such as in a file or database and are
151on a server that supports Mcrypt. "Light" use encryption such as
152encrypted session data or transitory encrypted flashdata require no
153intervention on your part. However, existing encrypted Sessions will be
154destroyed since data encrypted prior to 2.x will not be decoded.
155
Derek Jones3e9fb1c2011-10-05 16:12:36 -0500156..important:: **Why only a method to re-encode the data instead of maintaining legacy
Derek Jones8ede1a22011-10-05 13:34:52 -0500157methods for both encoding and decoding?** The algorithms in the
158Encryption library have improved in CodeIgniter 2.x both for performance
159and security, and we do not wish to encourage continued use of the older
160methods. You can of course extend the Encryption library if you wish and
161replace the new methods with the old and retain seamless compatibility
162with CodeIgniter 1.x encrypted data, but this a decision that a
163developer should make cautiously and deliberately, if at all.
164
165::
166
167 $new_data = $this->encrypt->encode_from_legacy($old_encrypted_string);
168
169Parameter
170Default
171Description
172**$orig_data**
173n/a
174The original encrypted data from CodeIgniter 1.x's Encryption library
175**$legacy_mode**
176MCRYPT_MODE_ECB
177The Mcrypt mode that was used to generate the original encrypted data.
178CodeIgniter 1.x's default was MCRYPT_MODE_ECB, and it will assume that
179to be the case unless overridden by this parameter.
180**$key**
181n/a
182The encryption key. This it typically specified in your config file as
183outlined above.