PHP兩種加密解密算法

一、Discuz中的加密解密函數---authcode

函數解釋:

$string:字符串,明文或密文

$operation:DECODE表示解密,其它表示加密

$key:密匙

$expiry:密文有效期

舉例:

$str ? ?= ?'lenovo';

$key ?= ?'https://365.lenovo.com.cn';

$authcode ?=? authcode($str,'ENCODE',$key,0); ?//加密

echo $authcode;

echo authcode($authcode,'DECODE',$key,0);//解密

用法:

function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

? ? ? ? ? ? ?// 動態密匙長度,相同的明文會生成不同密文就是依靠動態密匙

? ? ? ? ? ? ?$ckey_length = 4;

? ? ? ? ? ? ?// 密匙

? ? ? ? ? ? ?$key = md5($key ? $key : '設置默認值');

? ? ? ? ? ? ?// 密匙a會參與加解密

? ? ? ? ? ? ?$keya = md5(substr($key, 0, 16));

? ? ? ? ? ? ?// 密匙b會用來做數據完整性驗證

? ? ? ? ? ? ?$keyb = md5(substr($key, 16, 16));

? ? ? ? ? ? ?// 密匙c用于變化生成的密文

? ? ? ? ? ? ?$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, ? ? ? ? ? ? ?0, $ckey_length):

? ? ? ? ? ? ?substr(md5(microtime()), -$ckey_length)) : '';

? ? ? ? ? ? ?// 參與運算的密匙

? ? ? ? ? ? ?$cryptkey = $keya.md5($keya.$keyc);

? ? ? ? ? ? ?$key_length = strlen($cryptkey);

? ? ? ? ? ? ?// 明文,前10位用來保存時間戳,解密時驗證數據有效性,10到26位用來保存$keyb(密匙b),

? ? ? ? ? ? ?//解密時會通過這個密匙驗證數據完整性

? ? ? ? ? ? ?// 如果是解碼的話,會從第$ckey_length位開始,因為密文前$ckey_length位保存 動態密匙,以保證解密正確

? ? ? ? ? ? ?$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :

? ? ? ? ? ? ?sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb),0, 16).$string;

? ? ? ? ? ? ?$string_length = strlen($string);

? ? ? ? ? ? ?$result = '';

? ? ? ? ? ? ?$box = range(0, 255);

? ? ? ? ? ? ?$rndkey = array();

? ? ? ? ? ? ?// 產生密匙簿

? ? ? ? ? ? ?for($i = 0; $i <= 255; $i++) {

? ? ? ? ? ? ? ? ? ? ? ? ?$rndkey[$i] = ord($cryptkey[$i % $key_length]);

? ? ? ? ? ? ? }

? ? ? ? ? ? ? // 用固定的算法,打亂密匙簿,增加隨機性,好像很復雜,實際上對并不會增加密文的強度

? ? ? ? ? ? ? for($j = $i = 0; $i < 256; $i++) {

? ? ? ? ? ? ? ? ? ? ? ? ? $j = ($j + $box[$i] + $rndkey[$i]) % 256;

? ? ? ? ? ? ? ? ? ? ? ? ? $tmp = $box[$i];

? ? ? ? ? ? ? ? ? ? ? ? ? $box[$i] = $box[$j];

? ? ? ? ? ? ? ? ? ? ? ? ? $box[$j] = $tmp;

? ? ? ? ? ? ? }

? ? ? ? ? ? ?// 核心加解密部分

? ? ? ? ? ? ?for($a = $j = $i = 0; $i < $string_length; $i++) {

? ? ? ? ? ? ? ? ? ? ? ?$a = ($a + 1) % 256;

? ? ? ? ? ? ? ? ? ? ? ?$j = ($j + $box[$a]) % 256;

? ? ? ? ? ? ? ? ? ? ? ?$tmp = $box[$a];

? ? ? ? ? ? ? ? ? ? ? ?$box[$a] = $box[$j];

? ? ? ? ? ? ? ? ? ? ? ?$box[$j] = $tmp;

? ? ? ? ? ? ? ? ? ? ? ?// 從密匙簿得出密匙進行異或,再轉成字符

? ? ? ? ? ? ? ? ? ? ? ?$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));

? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?if($operation == 'DECODE') {

? ? ? ? ? ? ? ? ? ? ? ? // 驗證數據有效性,請看未加密明文的格式

? ? ? ? ? ? ? ? ? ? ? ? ?if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) ?&&?substr($result, 10, 16) == substr(md5(substr($result, ? ? ? ? ? 26).$keyb), 0, 16)) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return substr($result, 26);

? ? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?return '';

? ? ? ? ? ? ? ? ? ? ? ? ? ?}

? ? ? ? ? ? ? ?} else {

? ? ? ? ? ? ? ? ? ? ? ? ?// 把動態密匙保存在密文里,這也是為什么同樣的明文,生產不同密文后能解密的原因

? ? ? ? ? ? ? ? ? ? ? ? // 因為加密后的密文可能是一些特殊字符,復制過程可能會丟失,所以用base64編碼

? ? ? ? ? ? ? ? ? ? ? ? ?return $keyc.str_replace('=', '', base64_encode($result));

? ? ? ? ? ? ? ? }

}

二、php自帶的擴展openssl

1、安裝openssl

yum -y install openssl-devel

2、安裝php openssl

到php源碼包的目錄 ext/openssl 下執行:

/data/apps/php/bin/phpize

./configure --with-openssl --with-php-config=/data/apps/php/bin/php-config

make

make install

(可能需要mv config0.m4 config.m4)

把生成的 openssl.so 文件添加到php.ini中

extension = /xxx/openssl.so;

可以用以下命令查看安裝的模塊:

php -m |grep openssl

3、生成公鑰和私鑰

生成私鑰:

openssl genrsa 1024 > private.key (注意,1024是密鑰的長度,如果密鑰較長,相應加密后的密文也會較長)

生成公鑰:

openssl rsa -in private.key -pubout > public.key

class mycrypt {

? ? ? ? ? public $pubkey;

? ? ? ? ? public $privkey;

? ? ? ? ? function __construct() {

? ? ? ? ? ? ? ? ? ? $this->pubkey = file_get_contents('/home/openssl/public.key');

? ? ? ? ? ? ? ? ? ?$this->privkey = file_get_contents('/home/openssl/private.key');

? ? ? ? ? }

? ? ? ? ? public function encrypt($data) {

? ? ? ? ? ? ? ? ?if (openssl_public_encrypt($data, $encrypted, $this->pubkey))

? ? ? ? ? ? ? ? ? ? ? ? ? $data = base64_encode($encrypted);

? ? ? ? ? ? ? ? ?else

? ? ? ? ? ? ? ? ? ? ? ? ? throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');

? ? ? ? ? ? ? ? ? return $data;

? ? ? ? ? }

? ? ? ? ? public function decrypt($data) {

? ? ? ? ? ? ? ? ?if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))

? ? ? ? ? ? ? ? ? ? ? ? ? ? $data = $decrypted;

? ? ? ? ? ? ? ? ?else

? ? ? ? ? ? ? ? ? ? ? ? ? ? $data = '';

? ? ? ? ? ? ? ? ?return $data;

? ? ? ? ?}

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容