当前位置:   article > 正文

OpenSSL:DES加解密实战_openssl_decrypt(): failed to base64 decode the inp

openssl_decrypt(): failed to base64 decode the input

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

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

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

  1.     /**
  2.      * DES 解密函数
  3.      *
  4.      * @param  string  $ciphertext  密文
  5.      * @param  string  $method  加密学方式('DES-ECB')
  6.      * @param  string  $password  密钥  
  7.      */
  8.     function desecb_decrypt($ciphertext, $password, $method = 'DES-ECB', $options = OPENSSL_ZERO_PADDING)
  9.     {
  10.         $hex = hex2bin($ciphertext);
  11.         $data = base64_encode($hex);
  12.         $plaintext = openssl_decrypt($data, $method, $password, $options);
  13.         return trim($plaintext);
  14.     }
  15.     
  16.     /**
  17.      * DES 加密函数
  18.      *
  19.      * @param  string  $plaintext  明文
  20.      * @param  string  $method  解密方式('DES-ECB')
  21.      * @param  string  $password  密钥  
  22.      */
  23.     function desecb_encrypt($plaintext, $password, $method = 'DES-ECB', $options = OPENSSL_ZERO_PADDING)
  24.     {
  25.         //字符串有效长度要求是8的倍数,不够要空字符补足
  26.         if($m = strlen($plaintext)%8){
  27.             $plaintext .= str_repeat("\x00"8 - $m);//双字节字符
  28.         }
  29.         
  30.         $encResult = openssl_encrypt($plaintext, $method, $password, $options);
  31.         $ciphertext = bin2hex(base64_decode($encResult));
  32.         return $ciphertext;
  33.     }
  34.     
  35.     /**
  36.      * DES 解密函数
  37.      *
  38.      * @param  string  $ciphertext  密文
  39.      * @param  string  $method  加密方式('DES-CBC')
  40.      * @param  string  $password  密钥  
  41.      */
  42.     function descbc_ecrypt($ciphertext, $password, $method = 'DES-CBC', $options = 0, $iv = '')
  43.     {
  44.         if(empty($iv)){
  45.             $iv = $password;//如果偏移量传入为空,即默认填入密钥
  46.         }
  47.         $hex = hex2bin($ciphertext);
  48.         $data = base64_encode($hex);
  49.         $plaintext = openssl_decrypt($data, $method, $password, $options, $iv);
  50.         return $plaintext;
  51.     }
  52.     
  53.     /**
  54.      * DES 加密函数
  55.      *
  56.      * @param  string  $plaintext  明文
  57.      * @param  string  $method  解密方式('DES-CBC')
  58.      * @param  string  $password  密钥  
  59.      */
  60.     function descbc_encrypt($plaintext, $password, $method = 'DES-CBC', $options = 0, $iv = '')
  61.     {
  62.         if(empty($iv)){
  63.             $iv = $password;//如果偏移量传入为空,即默认填入密钥
  64.         }
  65.         $encResult = openssl_encrypt($plaintext, $method, $password, $options, $iv);
  66.         $ciphertext = bin2hex(base64_decode($encResult));
  67.         return $ciphertext;
  68.     }

    在实现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()函数去掉。


转载本文,请注明出处、作者。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/308419
推荐阅读
相关标签
  

闽ICP备14008679号