PHP中的多种加密技术及代码示例解析

2019-05-02 18:53:40王冬梅

非对称加密

非对称加密算法需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(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; 
?> 
								 
			 
相关文章 大家在看