加密類
數(shù)據(jù)加密類提供了兩種數(shù)據(jù)加密方式。 It uses a scheme that either compiles the message using a randomly hashed bitwise XOR encoding scheme, or is encrypted using the Mcrypt library. If Mcrypt is not available on your server the encoded message will still provide a reasonable degree of security for encrypted sessions or other such "light" purposes. If Mcrypt is available, you'll be provided with a high degree of security appropriate for storage.
設(shè)置你的密鑰
密鑰實(shí)際上是一些會(huì)控制密碼加密過(guò)程并且允許被加密的字串被解碼的信息片段。實(shí)際上,你選擇的密鑰會(huì)提供一個(gè)唯一的方法來(lái)解密一些被加密的數(shù)據(jù),所以你需要非常謹(jǐn)慎的設(shè)置你的密鑰,如果你想給一些固定的數(shù)據(jù)加密的話,你最好不要更改這個(gè)密鑰。
很自然,你需要非常小心的保守你的密鑰。如果某人對(duì)您的密鑰能夠存取,那么數(shù)據(jù)將會(huì)很容易地被解碼。如果您的服務(wù)器不完全在的您的控制之下而想保證數(shù)據(jù)安全是不可能的,因此您可以在使用它之前仔細(xì)地想一下要求高安全存放信用卡數(shù)字對(duì)象的方法。
為了發(fā)揮加密算法的最大優(yōu)勢(shì),你的解密密鑰需要被設(shè)置為 32 個(gè)字符長(zhǎng)度(128 位)。你可以設(shè)置一個(gè)編造的隨機(jī)字符串作為你的密鑰,最好包括數(shù)字、大寫(xiě)字母、小寫(xiě)字母。你的密鑰不能設(shè)置為一個(gè)簡(jiǎn)單的文本字符串。為了被安全可靠的加密,它也有一個(gè)隨機(jī)的可能性。
你的密鑰可以放在 application/config/config.php 文件中,你也可以自己設(shè)置一個(gè)存儲(chǔ)機(jī)制用于數(shù)據(jù)的加密和解密。
為了在 application/config/config.php 文件中保存你的密鑰,打開(kāi)文件設(shè)置一下:
$config['encryption_key'] = "YOUR KEY";
消息長(zhǎng)度
知道加密信息的長(zhǎng)度會(huì)是原來(lái)函數(shù)長(zhǎng)度的 2.6 倍是很重要的。如果你加密這個(gè)字符串“my super secret data”,它的長(zhǎng)度是 21 個(gè)字符,所以你加密后的字符串的長(zhǎng)度大概是 55 個(gè)字符(我們說(shuō)它是粗糙的,因?yàn)榫幋a的字符串長(zhǎng)度增量 64 位并非是線性增長(zhǎng)的),當(dāng)你選擇你的數(shù)據(jù)存儲(chǔ)機(jī)制的時(shí)候一定要記住這一點(diǎn)。例如,Cookie 可以占用 4k 的數(shù)據(jù)空間。
初始化類
在 Codeigniter 中,像大多數(shù)其他的類一樣,加密類也需要在你的控制器函數(shù)中用 $this->load->library 函數(shù)加載:
$this->load->library('encrypt');
一旦被加載,加密類庫(kù)就可以這樣使用:$this->encrypt
$this->encrypt->encode()
執(zhí)行數(shù)據(jù)加密并返回一個(gè)字符串。例如:
$msg = 'My secret message';
$encrypted_string = $this->encrypt->encode($msg);
如果你不想在你的配置文件中使用一個(gè)密鑰,你可以通過(guò)第二個(gè)參數(shù)隨意設(shè)置你的密鑰。
$msg = 'My secret message';
$key = 'super-secret-key';
$encrypted_string = $this->encrypt->encode($msg, $key);
$this->encrypt->decode()
解密一個(gè)已加密的字符串。例如:
$encrypted_string = 'APANtByIGI1BpVXZTJgcsAG8GZl8pdwwa84';
$plaintext_string = $this->encrypt->decode($encrypted_string);
You can optionally pass your encryption key via the second parameter if you don't want to use the one in your config file:
$msg = 'My secret message';
$key = 'super-secret-key';
$encrypted_string = $this->encrypt->decode($msg, $key);
$this->encrypt->set_cipher();
允許你設(shè)置一個(gè) Mcrypt 算法。默認(rèn)使用 MCRYPT_RIJNDAEL_256。例如:
$this->encrypt->set_cipher(MCRYPT_BLOWFISH);
請(qǐng)?jiān)L問(wèn) php.net 看一下可用的算法。
如果你想手動(dòng)測(cè)試一下你的服務(wù)器是否支持 Mcrypt,你可以使用:
echo ( ! function_exists('mcrypt_encrypt')) ? 'Nope' : 'Yup';
$this->encrypt->set_mode();
允許你設(shè)置一個(gè) Mcrypt 模式。默認(rèn)使用 MCRYPT_MODE_CBC。例如:
$this->encrypt->set_mode(MCRYPT_MODE_CFB);
請(qǐng)?jiān)L問(wèn) php.net 看一下可用的模式。
$this->encrypt->sha1();
SHA1 編碼函數(shù)。提供一個(gè)字符串,然后它返回一個(gè) 160 位的 Hash 信息。說(shuō)明:SHA1,就像 MD5 一樣不可解密。例如:
$hash = $this->encrypt->sha1('Some string');
許多 PHP 安裝程序默認(rèn)都支持 SHA1,所以你可以很簡(jiǎn)單的使用它的原始函數(shù)進(jìn)行加密:
$hash = sha1('Some string');
如果你的服務(wù)器不支持 SHA1,你可以使用別人提供的函數(shù)。
$this->encrypt->encode_from_legacy($orig_data, $legacy_mode = MCRYPT_MODE_ECB, $key = '');
Enables you to re-encode data that was originally encrypted with CodeIgniter 1.x to be compatible with the Encryption library in CodeIgniter 2.x. It is only necessary to use this method if you have encrypted data stored permanently such as in a file or database and are on a server that supports Mcrypt. "Light" use encryption such as encrypted session data or transitory encrypted flashdata require no intervention on your part. However, existing encrypted Sessions will be destroyed since data encrypted prior to 2.x will not be decoded.
Why only a method to re-encode the data instead of maintaining legacy methods for both encoding and decoding? The algorithms in the Encryption library have improved in CodeIgniter 2.x both for performance and security, and we do not wish to encourage continued use of the older methods. You can of course extend the Encryption library if you wish and replace the new methods with the old and retain seamless compatibility with CodeIgniter 1.x encrypted data, but this a decision that a developer should make cautiously and deliberately, if at all.
$new_data = $this->encrypt->encode_from_legacy($old_encrypted_string);
Parameter | Default | Description |
---|---|---|
$orig_data | n/a | The original encrypted data from CodeIgniter 1.x's Encryption library |
$legacy_mode | MCRYPT_MODE_ECB | The Mcrypt mode that was used to generate the original encrypted data. CodeIgniter 1.x's default was MCRYPT_MODE_ECB, and it will assume that to be the case unless overridden by this parameter. |
$key | n/a | The encryption key. This it typically specified in your config file as outlined above. |
?