OpenSSL:DES加解密实战

xiaoxiao2021-02-28  60

    DES加密算法,是一种对称加密算法,加解密都使用同一个密钥。

    OpenSSL扩展,是PHP常用的密码扩展之一。OpenSSL扩展封装了很多加密算法,而且不需要写很多复杂的代码,很多加密算法可以直接调用其函数实现。但是,在使用OpenSSL扩展的过程中,需要注意很多细节。如果不了解其函数的使用方法,具体传入参数的要求,可能要走很多弯路。

    最近,笔者用OpenSSL实现了DES的两种加密模式的加解密。

    /**      * DES 解密函数      *      * @param  string  $ciphertext  密文      * @param  string  $method  加密学方式('DES-ECB')      * @param  string  $password  密钥        */     function desecb_decrypt($ciphertext, $password, $method = 'DES-ECB', $options = OPENSSL_ZERO_PADDING)     {         $hex = hex2bin($ciphertext);         $data = base64_encode($hex);         $plaintext = openssl_decrypt($data, $method, $password, $options);         return trim($plaintext);     }          /**      * DES 加密函数      *      * @param  string  $plaintext  明文      * @param  string  $method  解密方式('DES-ECB')      * @param  string  $password  密钥        */     function desecb_encrypt($plaintext, $password, $method = 'DES-ECB', $options = OPENSSL_ZERO_PADDING)     {         //字符串有效长度要求是8的倍数,不够要空字符补足         if($m = strlen($plaintext)%8){             $plaintext .= str_repeat("\x00",  8 - $m);//双字节字符         }                  $encResult = openssl_encrypt($plaintext, $method, $password, $options);         $ciphertext = bin2hex(base64_decode($encResult));         return $ciphertext;     }          /**      * DES 解密函数      *      * @param  string  $ciphertext  密文      * @param  string  $method  加密方式('DES-CBC')      * @param  string  $password  密钥        */     function descbc_ecrypt($ciphertext, $password, $method = 'DES-CBC', $options = 0, $iv = '')     {         if(empty($iv)){             $iv = $password;//如果偏移量传入为空,即默认填入密钥         }         $hex = hex2bin($ciphertext);         $data = base64_encode($hex);         $plaintext = openssl_decrypt($data, $method, $password, $options, $iv);         return $plaintext;     }          /**      * DES 加密函数      *      * @param  string  $plaintext  明文      * @param  string  $method  解密方式('DES-CBC')      * @param  string  $password  密钥        */     function descbc_encrypt($plaintext, $password, $method = 'DES-CBC', $options = 0, $iv = '')     {         if(empty($iv)){             $iv = $password;//如果偏移量传入为空,即默认填入密钥         }         $encResult = openssl_encrypt($plaintext, $method, $password, $options, $iv);         $ciphertext = bin2hex(base64_decode($encResult));         return $ciphertext;     }

    在实现DES的加解密功能过程中,踏了不少坑,在此做个小小的总结。

        1、openssl_get_cipher_methods() 函数可获取有效密码方式列表,即$method的有效值,实现不同密码算法的加解密。        2、openssl_error_string() 函数可以获取OpenSSL加解密函数的报错信息,方便快速找到加密错误,进行调试。比如,调用descbc_encrypt()函数进行加密,并正确传入参数后,运行结果返回 false,那就可以调用这个函数去获取错误的信息了。        3、密钥 $password 的有效长度要求8个字符以上,不同的加密模式可能要求的有效长度也有区别。        4、在 DES-CBC 加密模式中,偏移量 $vi 一般填入密钥的字符。不同的函数,偏移量 $vi 填入的有效字长可能也有要求,使用openssl_cipher_vi_length()函数可以知悉。        5、传入的明文字符串 $plaintext 必须符合加密函数的有效长度要求。比如,在 DES-ECB 加密模式中,明文 $plaintext 必须经过处理,以达到字符串长度为有效的,即,8的倍数,以满足最终转换成byte字节类型的要求。        6、如果加密时,明文拼接了空字符码,那解密后,可调用trim()函数去掉。

转载本文,请注明出处、作者。
转载请注明原文地址: https://www.6miu.com/read-2625218.html

最新回复(0)