blob: 27c6a6484bd6c89b38e1a58a3d714cd812e921e7 [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
72 $msg = 'My secret message'; $encrypted_string = $this->encrypt->encode($msg);
73
74You can optionally pass your encryption key via the second parameter if
75you don't want to use the one in your config file::
76
77 $msg = 'My secret message'; $key = 'super-secret-key'; $encrypted_string = $this->encrypt->encode($msg, $key);
78
79$this->encrypt->decode()
80========================
81
82Decrypts an encoded string. Example::
83
84 $encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84'; $plaintext_string = $this->encrypt->decode($encrypted_string);
85
86You can optionally pass your encryption key via the second parameter if
87you don't want to use the one in your config file::
88
89 $msg = 'My secret message'; $key = 'super-secret-key'; $encrypted_string = $this->encrypt->decode($msg, $key);
90
91$this->encrypt->set_cipher();
92==============================
93
94Permits you to set an Mcrypt cipher. By default it uses
95MCRYPT_RIJNDAEL_256. Example::
96
97 $this->encrypt->set_cipher(MCRYPT_BLOWFISH);
98
99Please visit php.net for a list of `available
100ciphers <http://php.net/mcrypt>`_.
101
102If you'd like to manually test whether your server supports Mcrypt you
103can use::
104
105 echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';
106
107$this->encrypt->set_mode();
108============================
109
110Permits you to set an Mcrypt mode. By default it uses MCRYPT_MODE_CBC.
111Example::
112
113 $this->encrypt->set_mode(MCRYPT_MODE_CFB);
114
115Please visit php.net for a list of `available
116modes <http://php.net/mcrypt>`_.
117
118$this->encrypt->sha1();
119=======================
120
121SHA1 encoding function. Provide a string and it will return a 160 bit
122one way hash. Note: SHA1, just like MD5 is non-decodable. Example::
123
124 $hash = $this->encrypt->sha1('Some string');
125
126Many PHP installations have SHA1 support by default so if all you need
127is to encode a hash it's simpler to use the native function::
128
129 $hash = sha1('Some string');
130
131If your server does not support SHA1 you can use the provided function.
132
133$this->encrypt->encode_from_legacy($orig_data, $legacy_mode =
134MCRYPT_MODE_ECB, $key = '');
135==============================
136
137Enables you to re-encode data that was originally encrypted with
138CodeIgniter 1.x to be compatible with the Encryption library in
139CodeIgniter 2.x. It is only necessary to use this method if you have
140encrypted data stored permanently such as in a file or database and are
141on a server that supports Mcrypt. "Light" use encryption such as
142encrypted session data or transitory encrypted flashdata require no
143intervention on your part. However, existing encrypted Sessions will be
144destroyed since data encrypted prior to 2.x will not be decoded.
145
146**Why only a method to re-encode the data instead of maintaining legacy
147methods for both encoding and decoding?** The algorithms in the
148Encryption library have improved in CodeIgniter 2.x both for performance
149and security, and we do not wish to encourage continued use of the older
150methods. You can of course extend the Encryption library if you wish and
151replace the new methods with the old and retain seamless compatibility
152with CodeIgniter 1.x encrypted data, but this a decision that a
153developer should make cautiously and deliberately, if at all.
154
155::
156
157 $new_data = $this->encrypt->encode_from_legacy($old_encrypted_string);
158
159Parameter
160Default
161Description
162**$orig_data**
163n/a
164The original encrypted data from CodeIgniter 1.x's Encryption library
165**$legacy_mode**
166MCRYPT_MODE_ECB
167The Mcrypt mode that was used to generate the original encrypted data.
168CodeIgniter 1.x's default was MCRYPT_MODE_ECB, and it will assume that
169to be the case unless overridden by this parameter.
170**$key**
171n/a
172The encryption key. This it typically specified in your config file as
173outlined above.