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 字符的字符串,后者为了区别盐值

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

非对称加密

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

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

  • 乙方生成一对密钥(公钥和私钥)并将公钥向其它方公开。
  • 得到该公钥的甲方使用该密钥对机密信息进行加密后再发送给乙方。
  • 乙方再用自己保存的另一把专用密钥(私钥)对加密后的信息进行解密。乙方只能用其专用密钥(私钥)解密由对应的公钥加密后的信息。

在传输过程中,即使攻击者截获了传输的密文,并得到了乙的公钥,也无法破解密文,因为只有乙的私钥才能解密密文

同样,如果乙要回复加密信息给甲,那么需要甲先公布甲的公钥给乙用于加密,甲自己保存甲的私钥用于解密。

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

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

<?php 
/** 
* 使用openssl实现非对称加密 
* @since 2010-07-08 
*/ 
class Rsa { 
  /** 
   * private key 
   */ 
  private $_privKey; 
  /** 
   * public key 
   */ 
  private $_pubKey; 
  /** 
   * the keys saving path 
   */ 
  private $_keyPath; 
  /** 
   * the construtor,the param $path is the keys saving path 
   */ 
  public function __construct($path) { 
    if (emptyempty($path) || !is_dir($path)) { 
      throw new Exception('Must set the keys save path'); 
    } 
    $this->_keyPath = $path; 
  } 
  /** 
   * create the key pair,save the key to $this->_keyPath 
   */ 
  public function createKey() { 
    $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 
   */ 
  public function setupPrivKey() { 
    if (is_resource($this->_privKey)) { 
      return true; 
    } 
    $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'priv.key'; 
    $prk = file_get_contents($file); 
    $this->_privKey = openssl_pkey_get_private($prk); 
    return true; 
  } 
  /** 
   * setup the public key 
   */ 
  public function setupPubKey() { 
    if (is_resource($this->_pubKey)) { 
      return true; 
    } 
    $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'pub.key'; 
    $puk = file_get_contents($file); 
    $this->_pubKey = openssl_pkey_get_public($puk); 
    return true; 
  } 
  /** 
   * encrypt with the private key 
   */ 
  public function privEncrypt($data) { 
    if (!is_string($data)) { 
      return null; 
    } 
    $this->setupPrivKey(); 
    $r = openssl_private_encrypt($data, $encrypted, $this->_privKey); 
    if ($r) { 
      return base64_encode($encrypted); 
    } 
    return null; 
  } 
  /** 
   * decrypt with the private key 
   */ 
  public function privDecrypt($encrypted) { 
    if (!is_string($encrypted)) { 
      return null; 
    } 
    $this->setupPrivKey(); 
    $encrypted = base64_decode($encrypted); 
    $r = openssl_private_decrypt($encrypted, $decrypted, $this->_privKey); 
    if ($r) { 
      return $decrypted; 
    } 
    return null; 
  } 
  /** 
   * encrypt with public key 
   */ 
  public function pubEncrypt($data) { 
    if (!is_string($data)) { 
      return null; 
    } 
    $this->setupPubKey(); 
    $r = openssl_public_encrypt($data, $encrypted, $this->_pubKey); 
    if ($r) { 
      return base64_encode($encrypted); 
    } 
    return null; 
  } 
  /** 
   * decrypt with the public key 
   */ 
  public function pubDecrypt($crypted) { 
    if (!is_string($crypted)) { 
      return null; 
    } 
    $this->setupPubKey(); 
    $crypted = base64_decode($crypted); 
    $r = openssl_public_decrypt($crypted, $decrypted, $this->_pubKey); 
    if ($r) { 
      return $decrypted; 
    } 
    return null; 
  } 
  public function __destruct() { 
    @fclose($this->_privKey); 
    @fclose($this->_pubKey); 
  } 
} 
//以下是一个简单的测试demo,如果不需要请删除 
$rsa = new Rsa('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; 
?> 

对称加密算法

阅读剩余部分

相关阅读 >>

discuz!7.1、7.2 远程代码执行漏洞exp

discuz!x3.2版设置论坛qq在线客服号码无法发起聊天的问题解决办法

nginx支持.htaccess文件实现伪静态的方法分享

discuz! 远程附件设置简明教程

利用php自动生成印有用户信息的名片

phpmyadmin下载、安装和使用入门教程

开通使用七牛云存储的cdn来加速网站静态文件的图文教程

asp.net 独立discuz头像编辑模块分离打包

可以后台在线升级的建站程序cms大盘点(图文)

php实现scws中文分词搜索的方法

更多相关阅读请进入《Discuz论坛》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...