記錄 Openssl 替代 Mcrypt 3DES CBC 演算法解決方案

pijunkuan發表於2020-06-15

由於php 7.1 以後棄用了Mcrypt演算法,現在用openssl替代,經過測驗合格

上程式碼
Mcrypt演算法:

class Crypt3Des {

    var $key;//祕鑰
    var $iv;//填充向量

    //初始化
    function Crypt3Des($key, $iv){
        $this->key = $key;
        $this->iv = $iv;
    }

    //填充加密快的大小
    function pkcs5_pad ($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    //解密
    function decrypt($encrypted){
        $encrypted = pack('H*', $encrypted);
        $key = str_pad($this->key,24,'0');
        $td = mcrypt_module_open(MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
        if( $this->iv == '' )
        {
            $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }
        else
        {
            $iv = $this->iv;
        }
        //$ks = mcrypt_enc_get_bkey_size($td);
        @mcrypt_generic_init($td, $key, $iv);
        $decrypted = mdecrypt_generic($td, $encrypted);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $y=$this->pkcs5_unpad($decrypted);
        return $y;
    }

    //加密
    function encrypt($input){
        $size = mcrypt_get_block_size(MCRYPT_3DES,MCRYPT_MODE_CBC);
        $input = $this->pkcs5_pad($input, $size);
        $key = str_pad($this->key,24,'0');
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        if( $this->iv == '' )
        {
            $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }
        else
        {
            $iv = $this->iv;
        }
        echo "key:".$key."</br>";
        echo "iv:".$iv."</br>";
        echo "input:".$input."</br>";
        @mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = bin2hex($data);
        return $data;
    }




    function pkcs5_unpad($text){
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad){
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }

}

Openssl替代演算法

class Crypt3Des
{

    var $key;//祕鑰
    var $iv;//填充向量

    //初始化
    function __construct($key, $iv)
    {
        $this->key = $key;
        $this->iv = $iv;
    }

    //填充加密快的大小
    function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    //解密
    function decrypt($encrypted)
    {
        $key = str_pad($this->key, 24, '0');
        $encrypted = pack('H*', $encrypted);
        $size = openssl_cipher_iv_length("DES-EDE3-CBC");
        if ($this->iv == "") {
            $iv = openssl_random_pseudo_bytes($size);
        } else {
            $iv = $this->iv;
        }
        $rs = openssl_decrypt($encrypted, "DES-EDE3-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
        $rs = $this->pkcs5_unpad($rs);
        return $rs;
    }

    //加密
    function encrypt($input)
    {
        $key = str_pad($this->key, 24, '0');
        $size = openssl_cipher_iv_length("DES-EDE3-CBC");
        if ($this->iv == "") {
            $iv = openssl_random_pseudo_bytes($size);
        } else {
            $iv = $this->iv;
        }
        $input = $this->pkcs5_pad($input, $size);
        $rs = openssl_encrypt($input, "DES-EDE3-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
        $rs = bin2hex($rs);
        return $rs;
    }


    function pkcs5_unpad($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) {
            return false;
        }
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }

}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章