PHP加密算法

加密技術的重點是加密算法,加密算法主要分為三類:

對稱加密

非對稱加密

不可逆加密

對稱加密算法

加密過程:

將明文分成N個組,然后對各個組進行加密,形成各自的密文,最后把所有的分組密文進行合并,形成最終的密文。

優點:

算法公開、計算量小、加密速度快、加密效率高

缺點:

交易雙方都使用同樣鑰匙,安全性得不到保證

密鑰管理困難,尤其是在分布式網絡中

常用算法:

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);//創建初始向量$key_size = mcrypt_enc_get_key_size($td);//返回所支持的最大的密鑰長度(以字節計算)$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);//創建初始向量$key_size = mcrypt_enc_get_key_size($td);//返回所支持的最大的密鑰長度(以字節計算)$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 = "返回所支持的最大的密鑰長度(涉及到發件費啦";$data ="dadfafdafd,我是一個好孩子";$endata =? encrypt($key,$data);$data1 = decrypt($key,$endata);echo$endata;//直接輸出,在網頁上是亂碼,用base64_encode處理,就變成由字符、數組、加號、斜杠等共64種字符注冊echobase64_encode($endata);echo$data1;

非對稱加密算法

使用過程:

乙方生成兩把密鑰(公鑰和私鑰)

甲方獲取乙方的公鑰,然后用它對信息加密。

乙方得到加密后的信息,用私鑰解密,乙方也可用私鑰加密字符串

甲方獲取乙方私鑰加密數據,用公鑰解密

優點:

更安全,密鑰越長,它就越難破解

缺點:

加密速度慢

常用算法:

RSA、Elgamal、背包算法、Rabin、D-H、ECC(橢圓曲線加密算法)

RSA算法

<?php/**

* 使用openssl實現非對稱加密

*/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;?>

不可逆加密算法

加密過程中不需要使用密鑰,輸入明文后由系統直接經過加密算法處理成密文,這種加密后的數據是無法被解密的,只有重新輸入明文,并再次經過同樣不可逆的加密算法處理,得到相同的加密密文并被系統重新識別后,才能真正解密。

常用算法有? 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//這種方式的雙重加密也可以提高數據的安全性

作者:柳浪聞笛

鏈接:http://www.lxweimin.com/p/d10afbbb92e3

來源:簡書

簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

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

推薦閱讀更多精彩內容

  • 加密概要 在密碼學中,加密(英語:Encryption)是將明文信息改變為難以讀取的密文內容,使之不可讀的過程。只...
    Gundy_閱讀 634評論 2 9
  • /**ios常見的幾種加密方法: 普通的加密方法是講密碼進行加密后保存到用戶偏好設置( [NSUserDefaul...
    彬至睢陽閱讀 3,001評論 0 7
  • 一、Discuz中的加密解密函數---authcode 函數解釋: $string:字符串,明文或密文$opera...
    Uzero閱讀 1,444評論 0 1
  • 幾根舊鋼管,兩張廢鐵皮,花了好幾百大元做的鐵皮打孔字,請電焊小弟搗鼓出來這么個破玩意,權叫做門頭吧,馬峰問我:...
    守夜人雪諾閱讀 466評論 0 0
  • “佛說,前世五百次的回眸,才換來今生的一次擦肩而過。”所以前世的我定回眸張望了無數個“五百次”,方得以在今生與她來...
    莫負青檸閱讀 457評論 0 0