- <?php
-
// +----------------------------------------------------------------------+
-
// | Wiki Framework |
-
// +----------------------------------------------------------------------+
-
// $string 明文 或 密文 必填
-
// $isEncrypt 是否加密 可选,默认为加密
-
// $key 密匙 可选,默认为空格
-
// $b = gncrypt($a,TRUE,'123');
-
// $c = gncrypt($b,false,'123');
-
// 采用SHA1生成密匙簿,超过300个字符使用ZLIB压缩
-
function gncrypt($string, $isEncrypt = true, $key = KEY_SPACE) {
-
if (!isset($string{0}) || !isset($key{0})) {
-
return false;
-
}
-
$dynKey = $isEncrypt ? sha1(microtime(true)) : substr($string, 0, 40);
-
//$fixedKey = hash('sha1', $key);
-
$fixedKey = sha1($key);
-
$dynKeyPart1 = substr($dynKey, 0, 20);
-
$dynKeyPart2 = substr($dynKey, 20);
-
$fixedKeyPart1 = substr($fixedKey, 0, 20);
-
$fixedKeyPart2 = substr($fixedKey, 20);
-
$key = sha1($dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);
-
$string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));
-
$n = 0;
-
$result = '';
-
$len = strlen($string);
-
for ($n = 0; $n < $len; $n++) {
-
$result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));
-
}
-
return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);
-
}
-
-
$h = "hello world";
-
$a = gncrypt($h, true, "123");
-
echo $a."\n";
-
$b = gncrypt($a, false, "123");
-
echo $b."\b";
===========================================================================================
- <?php
-
-
/*/
-
# 函数功能:加密/解密
-
# 函数名称:zxingcrypt
-
# 参数表 :string date 要加密和解密的数据,$key 密钥,【, string mode = 'encode' 默认为加密 - 'decode'为解密】
-
# 返回值 :string
-
# 更新时间:Thu Mar 06 08:30:46 CST 2008
-
/*/
-
function gncrypt($data,$key='greenet',$mode = 'encode')
-
{
-
$key = md5($key);//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一
-
if ($mode == 'decode'){
-
$data = base64_decode($data);
-
}
-
if (function_exists('mcrypt_create_iv')){
-
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
-
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
-
}
-
if (isset($iv) && $mode == 'encode'){
-
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv);
-
}elseif (isset($iv) && $mode == 'decode'){
-
$passcrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv);
-
}
-
if ($mode == 'encode'){
-
$passcrypt = base64_encode($passcrypt);
-
}
-
return $passcrypt;
-
}
-
-
$encodestr = gncrypt('helloworld');
-
echo "base64=".base64_encode($encodestr)."
";
-
echo "string after encode=".$encodestr."
";
-
$decodestr = gncrypt($encodestr,'greenet','decode');
-
echo "decrypt string=".$decodestr."
";