• php 加密解密字符串


     1 /*********************************************************************
     2 函数名称:encrypt
     3 函数作用:加密解密字符串
     4 使用方法:
     5 加密     :encrypt('str','E','nowamagic');
     6 解密     :encrypt('被加密过的字符串','D','nowamagic');
     7 参数说明:
     8 $string   :需要加密解密的字符串
     9 $operation:判断是加密还是解密:E:加密   D:解密
    10 $key      :加密的钥匙(密匙);
    11  *********************************************************************/
    12     function encrypt($string, $operation, $key = 'WMqsfPpS9hwyoJnFP')
    13     {
    14         $key = md5($key);
    15         $key_length = strlen($key);
    16         $string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
    17         $string_length = strlen($string);
    18         $rndkey = $box = array();
    19         $result = '';
    20         for ($i = 0; $i <= 255; $i++) {
    21             $rndkey[$i] = ord($key[$i % $key_length]);
    22             $box[$i] = $i;
    23         }
    24         for ($j = $i = 0; $i < 256; $i++) {
    25             $j = ($j + $box[$i] + $rndkey[$i]) % 256;
    26             $tmp = $box[$i];
    27             $box[$i] = $box[$j];
    28             $box[$j] = $tmp;
    29         }
    30         for ($a = $j = $i = 0; $i < $string_length; $i++) {
    31             $a = ($a + 1) % 256;
    32             $j = ($j + $box[$a]) % 256;
    33             $tmp = $box[$a];
    34             $box[$a] = $box[$j];
    35             $box[$j] = $tmp;
    36             $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    37         }
    38         if ($operation == 'D') {
    39             if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
    40                 return substr($result, 8);
    41             } else {
    42                 return '';
    43             }
    44         } else {
    45             return str_replace('=', '', base64_encode($result));
    46         }
    47     }
  • 相关阅读:
    MyEclipse Tern was unable to complete your request in time
    12.5.2 访问被覆盖的方法
    猎头、培训与咨询的价值(2)【补1】——北漂18年(93)
    12.5.1 通过 @ISA 继承
    从柯洁对战AlphaGo,看商业智能
    jquery ajax POST 例子详解
    利用组合索引优化
    Oracle range 分区表
    Perl 面向对象上
    JSON导出CSV中文乱码解决方案
  • 原文地址:https://www.cnblogs.com/ygcool/p/5395233.html
Copyright © 2020-2023  润新知