php中加密解密DES类的简单使用方法示例

吾爱主题 阅读:142 2021-10-06 13:48:00 评论:0

本文实例讲述了php中加密解密des类的简单使用方法。分享给大家供大家参考,具体如下:

在平时的开发工作中,我们经常会对关键字符进行加密,可能为了安全 也可能为了规范,所以要正确使用des加密解密

代码1:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 class des {    var $key ; // 密钥    var $iv ; // 偏移量      function __construct( $key , $iv =0 ) {      $this ->key = $key ;      if ( $iv == 0 ) {          $this ->iv = $key ;        } else {          $this ->iv = $iv ;        // 创建初始向量, 并且检测密钥长度, windows 平台请使用 mcrypt_rand        // mcrypt_create_iv ( mcrypt_get_block_size (mcrypt_des, mcrypt_mode_cbc), mcrypt_dev_random );        }      }      function encrypt( $str ) {      //加密,返回大写十六进制字符串      $size = mcrypt_get_block_size ( mcrypt_des, mcrypt_mode_cbc );        $str = $this ->pkcs5pad ( $str , $size );   // bin2hex 把 ascii 字符的字符串转换为十六进制值      return strtoupper ( bin2hex( mcrypt_cbc(mcrypt_des, $this ->key, $str , mcrypt_encrypt, $this ->iv ) ) );      }      function decrypt( $str ) {      //解密        $strbin = $this ->hex2bin( strtolower ( $str ) );        $str = mcrypt_cbc( mcrypt_des, $this ->key, $strbin , mcrypt_decrypt, $this ->iv );        $str = $this ->pkcs5unpad( $str );        return $str ;      }        function hex2bin( $hexdata ) {        $bindata = "" ;        for ( $i = 0; $i < strlen ( $hexdata ); $i += 2) {          $bindata .= chr ( hexdec ( substr ( $hexdata , $i , 2 ) ) );        }        return $bindata ;      }      function pkcs5pad( $text , $blocksize ) {        $pad = $blocksize - ( strlen ( $text ) % $blocksize );        return $text . str_repeat ( chr ( $pad ), $pad );      }      function pkcs5unpad( $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 );    } }

deprecated: methods with the same name as their class will not be constructors in a future version of php; des5 has a deprecated constructor in d:\phpstudy_pro\www\des\des5.php on line 2

fatal error: uncaught error: call to undefined function mcrypt_get_block_size() in d:\phpstudy_pro\www\des\des5.php:19 stack trace: #0 d:\phpstudy_pro\www\des\1.php(10): des5->encrypt('podsmia') #1 {main} thrown in d:\phpstudy_pro\www\des\des5.php on line 19

  • mcrypt_cbc 以 cbc 模式加解密数据, 在php 5.5.0+被弃用, php 7.0.0被移除
  • mcrypt_encrypt / mcrypt_decrypt 使用给定参数加密 / 解密, 在php 7.1.0+被弃用, 在php 7.2.0+被移除

代码2:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class des7 {      //要改的加密, 使用 openssl      public function desencrypt( $str , $key ) {        $iv = $key ;        $data = openssl_encrypt( $str , "des-cbc" , $key ,openssl_raw_data, $iv );        $data = strtolower (bin2hex( $data ));        return $data ;      }      //要改的解密      public function desdecrypt( $str , $key ) {        $iv = $key ;        return openssl_decrypt (hex2bin( $str ), 'des-cbc' , $key , openssl_raw_data, $iv );      }   }

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_36261130/article/details/104015050

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容