110,570
社区成员
发帖
与我相关
我的任务
分享
<?php
require_once ("../basic/db_site.php");
class DES_Class
{
const ConstantKey = 'MMM';
const ConstantIV = 'UUU';
private $key = "";
private $iv = "";
function __construct ()
{
$this->key = $this->stringArrKey(base64_encode(self::ConstantKey));
$this->iv = $this->stringArrIv(self::ConstantIV); //$this->stringArrIv($iv);
}
private function stringArrKey($inputStr)
{
$stringArray[]=array();
$inputStr=ltrim($inputStr);
$inputStr=rtrim($inputStr);
$stringArray=str_split($inputStr);
$strLength=strlen($inputStr);
$strOutput[]=array();
if ($strLength<8)
{
for ($i=0;$i<$strLength;$i++)
{
$strOutput[$i]=$stringArray[$i];
}
for ($i=$strLength;$i<8;$i++)
{
$strOutput[$i]='0';
}
}
else if ($strLength>8)
{
for ($i=0;$i<8;$i++)
{
$strOutput[$i]=$stringArray[$i];
}
}
$strOutputFinal=implode("",$strOutput);
return $strOutputFinal;
}
public function CreateLic($message)
{
$messageArr[] = array();
$messageReplace[] = array();
$messageArr=str_split($message);
for ($i=0;$i<5;$i++)
{
$messageReplace[$i] = $messageArr[5+$i];
}
for ($i=0;$i<5;$i++)
{
$messageReplace[5+$i] = $messageArr[$i];
}
$messageReplace[10] = $messageArr[10];
for ($i=0;$i<5;$i++)
{
$messageReplace[11+$i] = $messageArr[16+$i];
}
for ($i=0;$i<5;$i++)
{
$messageReplace[16+$i] = $messageArr[11+$i];
}
$message=implode("",$messageReplace);
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = base64_decode($this->iv);
$message = $this->PaddingPKCS7($message);
$key = base64_decode($this->key);
mcrypt_generic_init($td, $key, $iv);
$result = base64_encode(mcrypt_generic($td, $message));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $result;
}
/***decode part-------public function*/
public function GetMessage ($license)
{
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = base64_decode($this->iv);
$key = base64_decode($this->key);
mcrypt_generic_init($td, $key, $iv);
$result = trim(mdecrypt_generic($td, base64_decode($license)));
$result = $this->UnPaddingPKCS7($result);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$licenseArr[] = array();
$licenseReplace[] = array();
$licenseArr=str_split($result);
for ($i=0;$i<5;$i++)
{
$licenseReplace[$i] = $licenseArr[5+$i];
}
for ($i=0;$i<5;$i++)
{
$licenseReplace[5+$i] = $licenseArr[$i];
}
$licenseReplace[10] = $licenseArr[10];
for ($i=0;$i<5;$i++)
{
$licenseReplace[11+$i] = $licenseArr[16+$i];
}
for ($i=0;$i<5;$i++)
{
$licenseReplace[16+$i] = $licenseArr[11+$i];
}
$result = implode("",$licenseReplace);
return $result;
}
private function PaddingPKCS7 ($data)
{
$block_size = mcrypt_get_block_size('tripledes', 'cbc');
$padding_char = $block_size - (strlen($data) % $block_size);
$data .= str_repeat(chr($padding_char), $padding_char);
return $data;
}
private function UnPaddingPKCS7($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);
}
private function stringArrIv($inputStr)
{
$stringArray[]=array();
$inputStr=ltrim($inputStr);
$inputStr=rtrim($inputStr);
$stringArray=str_split($inputStr);
$strLength=strlen($inputStr);
$strOutput[]=array();
if ($strLength<11)
{
for ($i=0;$i<$strLength;$i++)
{
$strOutput[$i]=$stringArray[$i];
}
for ($i=$strLength;$i<11;$i++)
{
$strOutput[$i]='0';
}
}
else if ($strLength>11)
{
for ($i=0;$i<11;$i++)
{
$strOutput[$i]=$stringArray[$i];
}
}
$strOutputFinal = implode("",$strOutput);
return $strOutputFinal;
}
public function outputPermit($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[]=array();
$strArr=str_split($clearMessage);
$letter = $strArr[0];
return $letter;
}
public function outputUserNum($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[]=array();
$strArr=str_split($clearMessage);
$strNum[]=array();
for ($i=1;$i<5;$i++)
{
$strNum[$i-1]=$strArr[$i];
}
$str_Num = implode("",$strNum);
$num = (int)$str_Num;
return $num;
}
public function outputStartDay($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[] = array();
$strArr = str_split($clearMessage);
$strStartDay[] = array();
$str_Num[] = array();
for ($i=5;$i<13;$i++)
{
$strStartDay[$i-5]=$strArr[$i];
}
for ($i=0;$i<4;$i++)
{
$str_Num[$i] = $strStartDay[$i];
}
$str_Num[4] = "-";
for ($i=5;$i<7;$i++)
{
$str_Num[$i] = $strStartDay[$i-1];
}
$str_Num[7] = "-";
for ($i=8;$i<10;$i++)
{
$str_Num[$i] = $strStartDay[$i-2];
}
$str_Num = implode("",$str_Num);
return $str_Num;
}
public function outputEndDay($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[] = array();
$strArr = str_split($clearMessage);
$strEndDay[] = array();
$str_Num[] = array();
for ($i=13;$i<21;$i++)
{
$strEndDay[$i-13]=$strArr[$i];
}
for ($i=0;$i<4;$i++)
{
$str_Num[$i] = $strEndDay[$i];
}
$str_Num[4] = "-";
for ($i=5;$i<7;$i++)
{
$str_Num[$i] = $strEndDay[$i-1];
}
$str_Num[7] = "-";
for ($i=8;$i<10;$i++)
{
$str_Num[$i] = $strEndDay[$i-2];
}
$str_Num = implode("",$str_Num);
return $str_Num;
}
public function outStartDay_num($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[] = array();
$strArr = str_split($clearMessage);
$strStartDay[] = array();
for ($i=5;$i<13;$i++)
{
$strStartDay[$i-5]=$strArr[$i];
}
$str_Num = implode("",$strStartDay);
$num = (int)$str_Num;
return $num;
}
public function outEndDay_num ($license)
{
$clearMessage = $this->GetMessage($license);
$strArr[] = array();
$strArr = str_split($clearMessage);
$strEndDay[] = array();
for ($i=13;$i<21;$i++)
{
$strEndDay[$i-13]=$strArr[$i];
}
$str_Num = implode("",$strEndDay);
$num = (int)$str_Num;
return $num;
}
function db_connect()
{
$con = mysqli_connect("localhost", "root", "root") or die ("You cannot connect the database!<br>".mysqli_error());
return $con;
}
public function user_count()
{
$con =$this-> db_connect();
$count_sql = "SELECT * FROM transport.user";
$result = mysqli_query($con,$count_sql);
$totalNum = mysqli_num_rows($result);
mysqli_close($con);
return $totalNum;
}
function getAll($query)
{
$con =$this-> db_connect();
$result = mysqli_query($con,$query);
if ($result)
$count = mysqli_num_rows($result);
$data = array();
for($i=0;$i<$count;$i++)
{
mysqli_data_seek($result,$i);
$data[$i] = mysqli_fetch_assoc($result);
}
mysqli_close($con);
return $data;
}
public function getLicense()
{
$con =$this->db_connect();
$count_sql = "SELECT license FROM transport.license ORDER BY No";
$datas =$this-> getAll($count_sql);
$new_Lic="";
foreach ($datas as $each)
$new_Lic = $each['license'];
mysqli_close($con);
return $new_Lic;
}
public function checkResult()
{
$existed_num = $this->user_count();
$license = $this->getLicense();
$allowed_num = $this->outputUserNum($license);
if ($existed_num < $allowed_num)
{
return true;
}
else
{
return false;
}
}
public function inputLic($license)
{
$con = $this->db_connect();
$insert_sql = "INSERT INTO transport.license (license) VALUES ('$license')";
$result = mysqli_query($con,$insert_sql);
mysqli_close($con);
if ($result)
{
return true;
}
else
{
return false;
}
}
public function checkExpiration($license)
{
$start = $this->outStartDay_num($license);
$end = $this-> outEndDay_num($license);
date_default_timezone_set('Europe/Paris');
$nowDay = (int)date("Ymd");
if (($nowDay >= $start) && ($nowDay <= $end))
{
return true;
}
else
{
return false;
}
}
}
function longinCheck()
{
$obj_login = new DES_Class();
$license = $obj_login->getLicense();
$time_period = $obj_login->checkExpiration($license);
if ($time_period)
{
return true;
}
else
{
return false;
}
}
?>
public function inputLic($license)
{
$con = $this->db_connect();
$insert_sql = "INSERT INTO transport.license (license) VALUES ('$license')";
$result = mysqli_query($con,$insert_sql);
mysqli_close($con);
if ($result)
{
return true;
}
else
{
return false;
}
}
另外,还有这个语句:
$count_sql = "SELECT license FROM transport.license ORDER BY No";
$datas =$this-> getAll($count_sql);
谢谢各位大神,感激啊!!!
/// <summary>
/// DES加密/解密类。
/// </summary>
public class DESEncrypt
{
public DESEncrypt()
{
}
#region ========加密========
/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, "密钥");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret=new StringBuilder();
foreach( byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}",b);
}
return ret.ToString();
}
#endregion
#region ========解密========
/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, "密钥");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len=Text.Length/2;
byte[] inputByteArray = new byte[len];
int x,i;
for(x=0;x<len;x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x]=(byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms=new System.IO.MemoryStream();
CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
#endregion
}
public sealed class DESEncryption
{
private static readonly string encryptKey = "YJSERVER";
private static readonly string decryptKey = "YJSERVER";
#region**********Method*******************************
/// <summary>
/// 加密字符串
/// 注意:密钥必须为8位
/// </summary>
/// <param name="strText">字符串</param>
/// <param name="encryptKey">密钥</param>
/// <return>加密后字符串</return>
public static string DesEncrypt(string strText)
{
string outString="";
byte[] byKey=null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, encryptKey.Length));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write) ;
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
outString = Convert.ToBase64String(ms.ToArray());
}
catch(System.Exception)
{
outString = "";
}
return outString;
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="strText">加了密的字符串</param>
/// <param name="decryptKey">密钥</param>
public static string DesDecrypt(string strText)
{
string outString = "";
byte[] byKey = null;
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
byte[] inputByteArray = new Byte[strText.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, decryptKey.Length));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
outString = encoding.GetString(ms.ToArray());
}
catch(System.Exception)
{
outString = "";
}
return outString;
}
#endregion
}
}