1. 程式人生 > >php對稱加密與非對稱加密

php對稱加密與非對稱加密

對稱加密:

<?php  
  
class mycrypt {  
  
    public $pubkey;  
    public $privkey;  
  
    function __construct() {  
                $this->pubkey = file_get_contents('./public.key');
                $this->privkey = file_get_contents('./private.key'); 
    }  
  
    public function encrypt($data) {  
        if (openssl_public_encrypt($data, $encrypted, $this->pubkey))  
            $data = base64_encode($encrypted);  
        else
            throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');  
  
        return $data;  
    }
    public function decrypt($data) {  
        if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))  
            $data = $decrypted;
        else
            $data = '';
        return $data;
    }
}
$rsa = new mycrypt();  
echo $a = $rsa -> encrypt('12345678');
echo '<hr/>';
echo $rsa -> decrypt($a); 
非對稱加密:
<?php
header('content-type:text/html;charset=utf-8');
/**
 * 使用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(empty($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 . 'private.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 .  'public.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('./');

//私鑰加密,公鑰解密
echo '中華人民共和國<br />';
$pre = $rsa->privEncrypt('中華人民共和國');
echo 'private encrypted:<br />' . $pre . '<br />';

$pud = $rsa->pubDecrypt($pre);
echo 'public decrypted:' . $pud . '<br />';

//公鑰加密,私鑰解密
echo 'source:我是寫PHP程式碼的<br />';
$pue = $rsa->pubEncrypt('我是寫PHP程式碼的');
echo 'public encrypt:<br />' . $pue . '<br />';

$prd = $rsa->privDecrypt($pue);
echo 'private decrypt:' . $prd;
?>