PHP經常使用的非對稱加密

加密算法的分類 
    1)不可逆加密算法 
    2)可逆加密算法 
           可逆加密算法又分爲兩大類:「對稱式」和「非對稱式」。 php

openssl 實現RSA非對稱加密算法

class Rsa
{
    //私匙
    private $privKey = '-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKk5rhfA/dMtae6F
1Fduwcnpskz/rAM15HvNY6B7814+GMRFF7/Vk7ELyHKjSF2Lekq9U4AEiJxBDvZR
nGgS4CiEw8v+hLL1zSR1/dTILMe0wHQyk9yOlU1x/l37M7NDrDxALYfrLj6pIVu1
7ysQiQz6WOFHM/XL2iGuel/wEd3pAgMBAAECgYEAqRnzZtc6jVkXPvvKyafqzGrT
+m+OoxKqhDAkuwzBj847j31/kvnIgSXGIxAZUyj6hk8UbpuZK5MzcV7yDWm3RgWa
xTIYpA4irvTY0coha8JWKLpCKA79sLaWi/CBQxMomHx5RApGepXrgnlpzr8ndy/4
6CET5i+N+xKQxE9Yz4UCQQDaQsO2BjpC1NN53wF7pHqs8RHYVmaIjKAvelCTQ8lQ
Ml0StJDwEr6ksTXvMShvcltA9L1FB/YA8B6Bt2t3Fn8TAkEAxnxgoNjbJERg24EP
sqvXqoFV9YwXJHeD56Ub8o4pbLV1eP8UFukV5o6Q93+uBpgeJNvG3xaHlmQz+y0D
EYxCkwJAJgd6CE4/eoOQsWxs2rlG8rkyTcqIJQSzqHIcdfxaqFGOLAMwOdRaSWHj
LuEW1YxMYB4EmHGRqIouFpX58I8H5wJADWbOe23I9hzr0UZYkf7KVAfhju/DjEHJ
lTVvuqPgpidmZCIM8DMSuHt9zEhBrs5Mkr6IIf36b6waWmZCo8etowJBAMXU+3ri
in7Rz3IMDOTJ7g29P42jMWDbBh98utqjt/0nVJAjYRSlnr92jSkXrPEZsvm8uejV
dHd+RNrkgV+DK9c=
-----END PRIVATE KEY-----';
    //公匙
    private $pubKey = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpOa4XwP3TLWnuhdRXbsHJ6bJM
/6wDNeR7zWOge/NePhjERRe/1ZOxC8hyo0hdi3pKvVOABIicQQ72UZxoEuAohMPL
/oSy9c0kdf3UyCzHtMB0MpPcjpVNcf5d+zOzQ6w8QC2H6y4+qSFbte8rEIkM+ljh
RzP1y9ohrnpf8BHd6QIDAQAB
-----END PUBLIC KEY-----';

    /**
     * the construtor,the param $path is the keys saving path
     */
    public function __construct()
    {
    }

    /**
     * encrypt with the private key
     */
    public function privEncrypt($data)
    {
        if(!is_string($data)){
            return null;
        }

        $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;
        }

        $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;
        }

        $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;
        }

        $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 '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;
相關文章
相關標籤/搜索