一、设置密钥:
密钥实际上是一些会控制密码加密过程并且允许被加密的字串被解码的信息片段。实际上,你选择的密钥会提供一个唯一的方法来解密一些被加密的数据,所以你需要非常谨慎的设置你的密钥,如果你想给一些固定的数据加密的话,你最好不要更改这个密钥。
为了发挥加密算法的最大优势,你的解密密钥需要被设置为 32 个字符长度(128 位)。你可以设置一个编造的随机字符串作为你的密钥,最好包括数字、大写字母、小写字母。你的密钥不能设置为一个简单的文本字符串。为了被安全可靠的加密,它也有一个随机的可能性。
你的密钥可以放在 application/config/config.php 文件中,你也可以自己设置一个存储机制用于数据的加密和解密。
为了在 application/config/config.php 文件中保存你的密钥,打开文件设置一下:
$config['encryption_key'] = "YOUR KEY";
二、对密钥进行md5加密:
function get_key($key = '')
{
if ($key == '')
{
if ($this->encryption_key != '')
{
return $this->encryption_key;
}
$CI =& get_instance();
$key = $CI->config->item('encryption_key');
if ($key == FALSE)
{
show_error('In order to use the encryption class requires that you set an encryption key in your config file.');
}
}
return md5($key);
}
三、加密类的加密函数:
function encode($string, $key = '')
{
$key = $this->get_key($key);
if ($this->_mcrypt_exists === TRUE)
{
$enc = $this->mcrypt_encode($string, $key);
}
else
{
$enc = $this->_xor_encode($string, $key);
}
return base64_encode($enc);
}
首先获取经过md5()加密后的密钥,然后如果服务器支持mcrypt加密,则用mcrypt函数加密,如果不支持则用私加密类私有成员方法_xor_encode($string, $key)加密,最后使用 MIME base64 对数据进行编码。
其中的mcrypt_encode($string, $key)代码如下:
function mcrypt_encode($data, $key)
{
$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
$init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
return $this->_add_cipher_noise($init_vect.mcrypt_encrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), $key);
}
该函数利用mcrypt建立向量初始化的值,然后再理由加密类私有成员方法_add_cipher_noise()进行加密。_add_cipher_noise()函数如下:
function _add_cipher_noise($data, $key)
{
$keyhash = $this->hash($key);
$keylen = strlen($keyhash);
$str = '';
for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
{
if ($j >= $keylen)
{
$j = 0;
}
$str .= chr((ord($data[$i]) + ord($keyhash[$j])) % 256);
}
return $str;
}
该函数首先利用成员方法hash()对传进来的参数$key进行hash加密,然后计算加密后的字符长度,然后生成加密字符串。该函数中还有一个成员方法hash(),代码如下:
function hash($str)
{
return ($this->_hash_type == 'sha1') ? $this->sha1($str) : md5($str);
}
该函数代码的作用是如果设置的_hash_type是sha1加密,则对其用sha1加密,如果_hash_type不是sha1则用md5()对字符加密。sha1()成员函数代码如下:
function sha1($str)
{
if ( ! function_exists('sha1'))
{
if ( ! function_exists('mhash'))
{
require_once(BASEPATH.'libraries/Sha1.php');
$SH = new CI_SHA;
return $SH->generate($str);
}
else
{
return bin2hex(mhash(MHASH_SHA1, $str));
}
}
else
{
return sha1($str);
}
}
四、解密过程:解密过程就是加密过程的逆向解密:
function decode($string, $key = '')
{
$key = $this->get_key($key);
if (preg_match('/[^a-zA-Z0-9\/\+=]/', $string))
{
return FALSE;
}
$dec = base64_decode($string);
if ($this->_mcrypt_exists === TRUE)
{
if (($dec = $this->mcrypt_decode($dec, $key)) === FALSE)
{
return FALSE;
}
}
else
{
$dec = $this->_xor_decode($dec, $key);
}
return $dec;
}
第一步:首先获取解密密钥;第二步:如果加密后的字符中有不是字母、数字、/、+、=,则返回false;第三部:使用 MIME base64 编码的数据进行解码;第四步:如果服务器支持mcrypt则对其进行mcrypt解密,如果不支持,则对其用私有成员方法_xor_decode($dec, $key)解密。mcrypt_decode()函数代码如下:
function mcrypt_decode($data, $key)
{
$data = $this->_remove_cipher_noise($data, $key);
$init_size = mcrypt_get_iv_size($this->_get_cipher(), $this->_get_mode());
if ($init_size > strlen($data))
{
return FALSE;
}
$init_vect = substr($data, 0, $init_size);
$data = substr($data, $init_size);
return rtrim(mcrypt_decrypt($this->_get_cipher(), $key, $data, $this->_get_mode(), $init_vect), "\0");
}
该函数首先利用私有成员方法_remove_cipher_noise($data, $key)进行一次解密,然后在用mcrypt函数进行一次解密后,并去除右边的空白得到解密后的字符。_remove_cipher_noise($data, $key)私有成员方法代码如下:
function _remove_cipher_noise($data, $key)
{
$keyhash = $this->hash($key);
$keylen = strlen($keyhash);
$str = '';
for ($i = 0, $j = 0, $len = strlen($data); $i < $len; ++$i, ++$j)
{
if ($j >= $keylen)
{
$j = 0;
}
$temp = ord($data[$i]) - ord($keyhash[$j]);
if ($temp < 0)
{
$temp = $temp + 256;
}
$str .= chr($temp);
}
return $str;
}
五:加密类的其他成员函数:
set_cipher($cipher):允许你设置一个 Mcrypt 算法。默认使用 MCRYPT_RIJNDAEL_256。例如:
$this->encrypt->set_cipher(MCRYPT_BLOWFISH);
set_mode($mode): 允许你设置一个 Mcrypt 模式。默认使用 MCRYPT_MODE_CBC 。例如:
$this->encrypt->set_mode(MCRYPT_MODE_CFB);
sha1($str):
SHA1 编码函数。提供一个字符串,然后它返回一个 160 位的 Hash 信息。说明:SHA1,就像 MD5 一样不可解密。例如:
$hash = $this->encrypt->sha1('Some string');
仅作为自己笔记之用。