151 lines
3.8 KiB
PHP
151 lines
3.8 KiB
PHP
<?php
|
|
if (!defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
class MyEncryption
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function keyED($txt, $encrypt_key)
|
|
{
|
|
$encrypt_key = md5($encrypt_key);
|
|
$ctr = 0;
|
|
$tmp = "";
|
|
for ($i = 0; $i < strlen($txt); $i++) {
|
|
if ($ctr == strlen($encrypt_key))
|
|
$ctr = 0;
|
|
$tmp .= substr($txt, $i, 1) ^ substr($encrypt_key, $ctr, 1);
|
|
$ctr++;
|
|
}
|
|
return $tmp;
|
|
}
|
|
|
|
public function encrypt($txt, $key)
|
|
{
|
|
$encrypt_key = md5(mt_rand(0, 100));
|
|
$ctr = 0;
|
|
$tmp = "";
|
|
for ($i = 0; $i < strlen($txt); $i++) {
|
|
if ($ctr == strlen($encrypt_key))
|
|
$ctr = 0;
|
|
$tmp .= substr($encrypt_key, $ctr, 1) . (substr($txt, $i, 1) ^ substr($encrypt_key, $ctr, 1));
|
|
$ctr++;
|
|
}
|
|
return $this->keyED($tmp, $key);
|
|
}
|
|
|
|
public function decrypt($txt, $key)
|
|
{
|
|
$txt = $this->keyED($txt, $key);
|
|
$tmp = "";
|
|
for ($i = 0; $i < strlen($txt); $i++) {
|
|
$md5 = substr($txt, $i, 1);
|
|
$i++;
|
|
$tmp .= (substr($txt, $i, 1) ^ $md5);
|
|
}
|
|
return $tmp;
|
|
}
|
|
|
|
/**
|
|
* Notes:url加key加密
|
|
* Created on: 2020/10/18 17:01
|
|
* Created by: dengbw
|
|
* @param $url
|
|
* @param $key
|
|
* @return string
|
|
*/
|
|
public function encrypt_url($url, $key)
|
|
{
|
|
return rawurlencode(base64_encode($this->encrypt($url, $key)));
|
|
}
|
|
|
|
/**
|
|
* Notes:url加key解密
|
|
* Created on: 2020/10/18 17:02
|
|
* Created by: dengbw
|
|
* @param $url
|
|
* @param $key
|
|
* @return string
|
|
*/
|
|
public function decrypt_url($url, $key)
|
|
{
|
|
return $this->decrypt(base64_decode(rawurldecode($url)), $key);
|
|
}
|
|
|
|
/**
|
|
* Notes:url加key解密数组
|
|
* Created on: 2020/10/18 17:02
|
|
* Created by: dengbw
|
|
* @param $url
|
|
* @param $key
|
|
* @return string
|
|
*/
|
|
public function get_url($str, $key)
|
|
{
|
|
$str = $this->decrypt_url($str, $key);
|
|
$url_array = explode('&', $str);
|
|
if (is_array($url_array)) {
|
|
foreach ($url_array as $var) {
|
|
$var_array = explode("=", $var);
|
|
$vars [$var_array [0]] = $var_array [1];
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
/**
|
|
* Notes:url加密
|
|
* Created on: 2020/10/18 17:00
|
|
* Created by: dengbw
|
|
* @param $data
|
|
* @return string
|
|
*/
|
|
public function base64url_encode($data)
|
|
{
|
|
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
|
|
}
|
|
|
|
/**
|
|
* Notes:url解密
|
|
* Created on: 2020/10/18 17:01
|
|
* Created by: dengbw
|
|
* @param $data
|
|
* @return array
|
|
*/
|
|
public function base64url_decode($data)
|
|
{
|
|
$str = base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
|
|
$url_array = explode('&', $str);
|
|
$vars = array();
|
|
if (is_array($url_array)) {
|
|
foreach ($url_array as $var) {
|
|
$var_array = explode("=", $var);
|
|
$vars[$var_array[0]] = $var_array[1];
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
/**
|
|
* Notes:随机生成数字字母组合
|
|
* Created on: 2020/11/4 12:40
|
|
* Created by: dengbw
|
|
* @param $len
|
|
* @param null $chars
|
|
* @return string
|
|
*/
|
|
public function random_string($len, $chars = null)
|
|
{
|
|
if (is_null($chars)) {
|
|
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
}
|
|
mt_srand(10000000 * (double)microtime());
|
|
for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {
|
|
$str .= $chars[mt_rand(0, $lc)];
|
|
}
|
|
return $str;
|
|
}
|
|
}
|