加密技術的重點是加密算法,加密算法主要分為三類:
對稱加密
非對稱加密
不可逆加密
對稱加密算法
加密過程:
將明文分成N個組蚯撩,然后對各個組進行加密莫矗,形成各自的密文,最后把所有的分組密文進行合并柄驻,形成最終的密文狐树。
優(yōu)點:
算法公開、計算量小鸿脓、加密速度快抑钟、加密效率高
缺點:
交易雙方都使用同樣鑰匙,安全性得不到保證
密鑰管理困難野哭,尤其是在分布式網絡中
常用算法:
DES在塔、3DES(TripleDES)、AES拨黔、RC2蛔溃、RC4、RC5和Blowfish
PHP中對稱加密算法
$cipher_list = mcrypt_list_algorithms();//mcrypt支持的加密算法列表$mode_list = mcrypt_list_modes();//mcrypt支持的加密模式列表// print_r($cipher_list);// print_r($mode_list);functionencrypt($key,$data){? ? $td = mcrypt_module_open("des","","ecb","");//使用MCRYPT_DES算法,ecb模式$size = mcrypt_enc_get_iv_size($td);//設置初始向量的大小$iv = mcrypt_create_iv($size,MCRYPT_RAND);//創(chuàng)建初始向量$key_size = mcrypt_enc_get_key_size($td);//返回所支持的最大的密鑰長度(以字節(jié)計算)$salt ='';? ? $subkey = substr(md5(md5($key).$salt),0,$key_size);//對key復雜處理,并設置長度mcrypt_generic_init($td, $subkey, $iv);? ? $endata = mcrypt_generic($td, $data);? ? mcrypt_generic_deinit($td);? ? mcrypt_module_close($td);return$endata;}functiondecrypt($key,$endata){? ? $td = mcrypt_module_open("des","","ecb","");//使用MCRYPT_DES算法,ecb模式$size = mcrypt_enc_get_iv_size($td);//設置初始向量的大小$iv = mcrypt_create_iv($size,MCRYPT_RAND);//創(chuàng)建初始向量$key_size = mcrypt_enc_get_key_size($td);//返回所支持的最大的密鑰長度(以字節(jié)計算)$salt ='';? ? $subkey = substr(md5(md5($key).$salt),0,$key_size);//對key復雜處理城榛,并設置長度mcrypt_generic_init($td, $subkey, $iv);? ? $data = rtrim(mdecrypt_generic($td, $endata)).'\n';? ? mcrypt_generic_deinit($td);? ? mcrypt_module_close($td);return$data;}$key ="www.tencent.com";// $data = "返回所支持的最大的密鑰長度(涉及到發(fā)件費啦";$data ="dadfafdafd,我是一個好孩子";$endata =? encrypt($key,$data);$data1 = decrypt($key,$endata);echo$endata;//直接輸出揪利,在網頁上是亂碼,用base64_encode處理狠持,就變成由字符疟位、數(shù)組、加號喘垂、斜杠等共64種字符注冊echobase64_encode($endata);echo$data1;
非對稱加密算法
使用過程:
乙方生成兩把密鑰(公鑰和私鑰)
甲方獲取乙方的公鑰甜刻,然后用它對信息加密。
乙方得到加密后的信息正勒,用私鑰解密得院,乙方也可用私鑰加密字符串
甲方獲取乙方私鑰加密數(shù)據(jù),用公鑰解密
優(yōu)點:
更安全章贞,密鑰越長祥绞,它就越難破解
缺點:
加密速度慢
常用算法:
RSA、Elgamal鸭限、背包算法蜕径、Rabin、D-H败京、ECC(橢圓曲線加密算法)
RSA算法
<?php/**
* 使用openssl實現(xiàn)非對稱加密
*/classRsa{/**
? ? * private key
? ? */private$_privKey;/**
? ? * public key
? ? */private$_pubKey;/**
? ? * the keys saving path
? ? */private$_keyPath;/**
? ? * the construtor,the param $path is the keys saving path
? ? */publicfunction__construct($path){if(empty($path) || !is_dir($path)) {thrownewException('Must set the keys save path');? ? ? ? }$this->_keyPath = $path;? ? }/**
? ? * create the key pair,save the key to $this->_keyPath
? ? * 也可以使用openssl命令生成公鑰私鑰
? ? *? openssl genrsa -out rsa_private_key.pem 1024
? ? *? openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
? ? */publicfunctioncreateKey(){? ? ? ? $r = openssl_pkey_new();? ? ? ? openssl_pkey_export($r, $privKey);? ? ? ? file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR .'priv.key', $privKey);$this->_privKey = openssl_pkey_get_public($privKey);? ? ? ? $rp = openssl_pkey_get_details($r);? ? ? ? $pubKey = $rp['key'];? ? ? ? file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR .'pub.key', $pubKey);$this->_pubKey = openssl_pkey_get_public($pubKey);? ? }/**
? ? * setup the private key
? ? */publicfunctionsetupPrivKey(){if(is_resource($this->_privKey)) {returntrue;? ? ? ? }? ? ? ? $file =$this->_keyPath . DIRECTORY_SEPARATOR .'priv.key';? ? ? ? $prk = file_get_contents($file);$this->_privKey = openssl_pkey_get_private($prk);returntrue;? ? }/**
? ? * setup the public key
? ? */publicfunctionsetupPubKey(){if(is_resource($this->_pubKey)) {returntrue;? ? ? ? }? ? ? ? $file =$this->_keyPath . DIRECTORY_SEPARATOR .'pub.key';? ? ? ? $puk = file_get_contents($file);$this->_pubKey = openssl_pkey_get_public($puk);returntrue;? ? }/**
? ? * encrypt with the private key
? ? */publicfunctionprivEncrypt($data){if(!is_string($data)) {returnnull;? ? ? ? }$this->setupPrivKey();? ? ? ? $r = openssl_private_encrypt($data, $encrypted,$this->_privKey);if($r) {returnbase64_encode($encrypted);? ? ? ? }returnnull;? ? }/**
? ? * decrypt with the private key
? ? */publicfunctionprivDecrypt($encrypted){if(!is_string($encrypted)) {returnnull;? ? ? ? }$this->setupPrivKey();? ? ? ? $encrypted = base64_decode($encrypted);? ? ? ? $r = openssl_private_decrypt($encrypted, $decrypted,$this->_privKey);if($r) {return$decrypted;? ? ? ? }returnnull;? ? }/**
? ? * encrypt with public key
? ? */publicfunctionpubEncrypt($data){if(!is_string($data)) {returnnull;? ? ? ? }$this->setupPubKey();? ? ? ? $r = openssl_public_encrypt($data, $encrypted,$this->_pubKey);if($r) {returnbase64_encode($encrypted);? ? ? ? }returnnull;? ? }/**
? ? * decrypt with the public key
? ? */publicfunctionpubDecrypt($crypted){if(!is_string($crypted)) {returnnull;? ? ? ? }$this->setupPubKey();? ? ? ? $crypted = base64_decode($crypted);? ? ? ? $r = openssl_public_decrypt($crypted, $decrypted,$this->_pubKey);if($r) {return$decrypted;? ? ? ? }returnnull;? ? }publicfunction__destruct(){? ? ? ? @fclose($this->_privKey);? ? ? ? @fclose($this->_pubKey);? ? }}//以下是一個簡單的測試demo兜喻,如果不需要請刪除$rsa =newRsa('ssl-key');//私鑰加密,公鑰解密echo'source:我是老鱉<br />';$pre = $rsa->privEncrypt('我是老鱉');echo'private encrypted:<br />'. $pre .'<br />';$pud = $rsa->pubDecrypt($pre);echo'public decrypted:'. $pud .'<br />';//公鑰加密赡麦,私鑰解密echo'source:干IT的<br />';$pue = $rsa->pubEncrypt('干IT的');echo'public encrypt:<br />'. $pue .'<br />';$prd = $rsa->privDecrypt($pue);echo'private decrypt:'. $prd;?>
不可逆加密算法
加密過程中不需要使用密鑰朴皆,輸入明文后由系統(tǒng)直接經過加密算法處理成密文,這種加密后的數(shù)據(jù)是無法被解密的泛粹,只有重新輸入明文遂铡,并再次經過同樣不可逆的加密算法處理,得到相同的加密密文并被系統(tǒng)重新識別后晶姊,才能真正解密忧便。
常用算法有? md5, crypt,sha1
md5
<?php$data ='hello';echomd5($data);//輸出32位的16進制 5d41402abc4b2a76b9719d911017c592
crypt
//以不同散列類型使用 crypt()以上輸出Standard DES: rl.3StKT.4T8MExtended DES: _J9..rasmBYk8r9AiWNcMD5:$1$rasmusle$rISCgZzpwk3UhDidwXvin0Blowfish:$2a$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hiSHA-256:$5$rounds=5000$usesomesillystri$KqJWpanXZHKq2BOB43TSaYhEWsQ1Lr5QNyPCDH/Tp.6SHA-512:$6$rounds=5000$usesomesillystri$D4IrlXatmP7rx3P3InaxBeoomnAihCKRVQP22JZ6EY47Wc6BkroIuUUBOov1i.S5KPgErtP/EN5mcO.ChWQW21
sha1
<?php$data="hello";echosha1($data);// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d//當然,可以將多種加密算法混合使用echomd5(sha1($data));//輸出:e69d7e620e82be5eb414d1f8d1d4b9d9//這種方式的雙重加密也可以提高數(shù)據(jù)的安全性
作者:柳浪聞笛
鏈接:http://www.reibang.com/p/d10afbbb92e3
來源:簡書
簡書著作權歸作者所有帽借,任何形式的轉載都請聯(lián)系作者獲得授權并注明出處。