歪着看世界 2013年09月30日
如何php代码转化为C#代码?
各位大神,小弟入行的月数可以用一个指头算出来,主要熟悉PHP,经验尚浅。 前不久利用DES加密代码写了一个类。现在,出于需求,我需要在短期内把这个类在.net平台上用C#重新写出来。我对.net以及C#毫无所知(当然知道有这么一个东西),请问大神们有什么办法可以把php无差错的转化为C#代码呢?

请大神们救救急,赐教工具、方法甚至代码。感激涕零。这里问问题,好像最多是100分,我就全加上。
下面是php代码:


<?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;
}


}




?>

...全文
283 1 收藏 9
写回复
9 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C#
创建于2007-09-28

8.4w+

社区成员

64.0w+

社区内容

.NET技术 C#
社区公告
暂无公告