加入收藏 | 设为首页 | 会员中心 | 我要投稿 南平站长网 (https://www.0599zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

浅谈 PHP 中的多种加密技术及代码示例

发布时间:2016-09-29 02:32:54 所属栏目:PHP教程 来源:菜问
导读:副标题#e# 同样是一道面试答错的问题,面试官问我非对称加密算法中有哪些经典的算法? 当时我愣了一下,因为我把非对称加密与单项散列加密的概念弄混淆了,所以更不用说什么非对称加密算法中有什么经典算法,结果当然也让面试官愣了一下,所以今天就花点时
副标题[/!--empirenews.page--]

同样是一道面试答错的问题,面试官问我非对称加密算法中有哪些经典的算法? 当时我愣了一下,因为我把非对称加密与单项散列加密的概念弄混淆了,所以更不用说什么非对称加密算法中有什么经典算法,结果当然也让面试官愣了一下,所以今天就花点时间说说PHP中的信息加密技术

信息加密技术的分类

单项散列加密技术(不可逆的加密)

属于摘要算法,不是一种加密算法,作用是把任意长的输入字符串变化成固定长的输出串的一种函数

MD5

string md5 ( string $str [, bool $raw_output = false ] ); //MD5加密,输入任意长度字符串返回一个唯一的32位字符

md5()为单向加密,没有逆向解密算法,但是还是可以对一些常见的字符串通过收集,枚举,碰撞等方法破解;所以为了让其破解起来更麻烦一些,所以我们一般加一点盐值(salt)并双重MD5;

md5(md5($password).'sdva');

sdva就是盐值,该盐值应该是随机的,比如md5常用在密码加密上,所以在注册的时候我会随机生成这个字符串,然后通过上面的方法来双重加密一下;

Crypt

很少看到有人用这个函数,如果要用的话有可能是用在对称或非对称的算法里面,了解一下既可;

string crypt ( string $str [, string $salt ] ) //第一个为需要加密的字符串,第二个为盐值(就是加密干扰值,如果没有提供,则默认由PHP自动生成);返回散列后的字符串或一个少于 13 字符的字符串,后者为了区别盐值

  1. <?php 
  2. $password='testtest.com'; 
  3. echo crypt($password); 
  4. //输出:$1$DZ3.QX2.$CQZ8I.OfeepKYrWp0oG8L1 
  5. /*第二个$与第三个$之间的八个字符是由PHP生成的,每刷新一次就变一次 
  6. */ 
  7. echo "<hr>"; 
  8.  
  9. echo crypt($password,"testtest"); 
  10. //输出:tesGeyALKYm3A 
  11. //当我们要加自定义的盐值时,如例子中的testtest作为第二个参数直接加入, 超出两位字符的会截取前两位 
  12. echo "<hr>"; 
  13.  
  14. echo  crypt($password,'$1$testtest$'); 
  15. //输出:$1$testtest$DsiRAWGTHiVH3O0HSHGoL1 
  16. /*crypt加密函数有多种盐值加密支持,以上例子展示的是MD5散列作为盐值,该方式下 
  17. 盐值以$1$$的形式加入,如例子中的testtest加在后两个$符之间, 
  18. 超出八位字符的会截取前八位,总长为12位;crypt默认就是这种形式。 
  19. */ 
  20. echo "<hr>"; 
  21. //crypt还有多种盐值加密支持,详见手册 
  22.  
  23. Sha1加密: 
  24.  
  25. string sha1 ( string $str [, bool $raw_output = false ]); //跟md5很像,不同的是sha1()默认情况下返回40个字符的散列值,传入参数性质一样,第一个为加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,sha1()则会返回原始的20 位原始格式报文摘要 
  26. <?php 
  27. $my_intro="zhouxiaogang"; 
  28. echo sha1($my_intro); // b6773e8c180c693d9f875bcf77c1202a243e8594 
  29. echo "<hr>"; 
  30. //当然,可以将多种加密算法混合使用 
  31. echo md5(sha1($my_intro)); 
  32. //输出:54818bd624d69ac9a139bf92251e381d 
  33. //这种方式的双重加密也可以提高数据的安全性 

非对称加密

非对称加密算法需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(public key,简称公钥)和私有密钥(private key,简称私钥);

1b4c510fd9f9d72a79ac165bd72a2834359bbbaf.jpg-182.2kB

如图所示,甲乙之间使用非对称加密的方式完成了重要信息的安全传输。

  1. 乙方生成一对密钥(公钥和私钥)并将公钥向其它方公开。

  2. 得到该公钥的甲方使用该密钥对机密信息进行加密后再发送给乙方。

  3. 乙方再用自己保存的另一把专用密钥(私钥)对加密后的信息进行解密。乙方只能用其专用密钥(私钥)解密由对应的公钥加密后的信息。

在传输过程中,即使攻击者截获了传输的密文,并得到了乙的公钥,也无法破解密文,因为只有乙的私钥才能解密密文
同样,如果乙要回复加密信息给甲,那么需要甲先公布甲的公钥给乙用于加密,甲自己保存甲的私钥用于解密。

在非对称加密中使用的主要算法有:RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法)等。 其中我们最见的算法是RSA算法

以下是从网上摘抄的一段PHP通过openssl实现非对称加密的算法

  1. <?php 
  2. /** 
  3. * 使用openssl实现非对称加密 
  4. * @since 2010-07-08 
  5. */ 
  6. class Rsa { 
  7.     /** 
  8.      * private key 
  9.      */ 
  10.     private $_privKey; 
  11.     /** 
  12.      * public key 
  13.      */ 
  14.     private $_pubKey; 
  15.     /** 
  16.      * the keys saving path 
  17.      */ 
  18.     private $_keyPath; 
  19.     /** 
  20.      * the construtor,the param $path is the keys saving path 
  21.      */ 
  22.     public function __construct($path) { 
  23.         if (emptyempty($path) || !is_dir($path)) { 
  24.             throw new Exception('Must set the keys save path'); 
  25.         } 
  26.         $this->_keyPath = $path; 
  27.     } 
  28.     /** 
  29.      * create the key pair,save the key to $this->_keyPath 
  30.      */ 
  31.     public function createKey() { 
  32.         $r = openssl_pkey_new(); 
  33.         openssl_pkey_export($r, $privKey); 
  34.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); 
  35.         $this->_privKey = openssl_pkey_get_public($privKey); 
  36.         $rp = openssl_pkey_get_details($r); 
  37.         $pubKey = $rp['key']; 
  38.         file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); 
  39.         $this->_pubKey = openssl_pkey_get_public($pubKey); 
  40.     } 
  41.     /** 
  42.      * setup the private key 
  43.      */ 
  44.     public function setupPrivKey() { 
  45.         if (is_resource($this->_privKey)) { 
  46.             return true; 
  47.         } 
  48.         $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; 
  49.         $prk = file_get_contents($file); 
  50.         $this->_privKey = openssl_pkey_get_private($prk); 
  51.         return true; 
  52.     } 
  53.     /** 
  54.      * setup the public key 
  55.      */ 
  56.     public function setupPubKey() { 
  57.         if (is_resource($this->_pubKey)) { 
  58.             return true; 
  59.         } 
  60.         $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; 
  61.         $puk = file_get_contents($file); 
  62.         $this->_pubKey = openssl_pkey_get_public($puk); 
  63.         return true; 
  64.     } 
  65.     /** 
  66.      * encrypt with the private key 
  67.      */ 
  68.     public function privEncrypt($data) { 
  69.         if (!is_string($data)) { 
  70.             return null; 
  71.         } 
  72.         $this->setupPrivKey(); 
  73.         $r = openssl_private_encrypt($data, $encrypted, $this->_privKey); 
  74.         if ($r) { 
  75.             return base64_encode($encrypted); 
  76.         } 
  77.         return null; 
  78.     } 
  79.     /** 
  80.      * decrypt with the private key 
  81.      */ 
  82.     public function privDecrypt($encrypted) { 
  83.         if (!is_string($encrypted)) { 
  84.             return null; 
  85.         } 
  86.         $this->setupPrivKey(); 
  87.         $encrypted = base64_decode($encrypted); 
  88.         $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); 
  89.         if ($r) { 
  90.             return $decrypted; 
  91.         } 
  92.         return null; 
  93.     } 
  94.     /** 
  95.      * encrypt with public key 
  96.      */ 
  97.     public function pubEncrypt($data) { 
  98.         if (!is_string($data)) { 
  99.             return null; 
  100.         } 
  101.         $this->setupPubKey(); 
  102.         $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey); 
  103.         if ($r) { 
  104.             return base64_encode($encrypted); 
  105.         } 
  106.         return null; 
  107.     } 
  108.     /** 
  109.      * decrypt with the public key 
  110.      */ 
  111.     public function pubDecrypt($crypted) { 
  112.         if (!is_string($crypted)) { 
  113.             return null; 
  114.         } 
  115.         $this->setupPubKey(); 
  116.         $crypted = base64_decode($crypted); 
  117.         $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); 
  118.         if ($r) { 
  119.             return $decrypted; 
  120.         } 
  121.         return null; 
  122.     } 
  123.     public function __destruct() { 
  124.         @fclose($this->_privKey); 
  125.         @fclose($this->_pubKey); 
  126.     } 
  127. //以下是一个简单的测试demo,如果不需要请删除 
  128. $rsa = new Rsa('ssl-key'); 
  129. //私钥加密,公钥解密 
  130. echo 'source:我是老鳖<br />'; 
  131. $pre = $rsa->privEncrypt('我是老鳖'); 
  132. echo 'private encrypted:<br />' . $pre . '<br />'; 
  133. $pud = $rsa->pubDecrypt($pre); 
  134. echo 'public decrypted:' . $pud . '<br />'; 
  135. //公钥加密,私钥解密 
  136. echo 'source:干IT的<br />'; 
  137. $pue = $rsa->pubEncrypt('干IT的'); 
  138. echo 'public encrypt:<br />' . $pue . '<br />'; 
  139. $prd = $rsa->privDecrypt($pue); 
  140. echo 'private decrypt:' . $prd; 
  141. ?> 

对称加密算法

(编辑:南平站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读