一段javascript轉PHP的encrypt加密算法

javascript 常用函數轉換成PHP代碼處理

class js2php
{
public static function detect_encoding($text){
$charsets = array(“UTF-8”,“GB2312”,“GBK”,“BIG5”,“ASCII”,“EUC-JP”,‘KOI8-R’,“Shift_JIS”,“CP936”,“ISO-8859-1”,“JIS”,“eucjp-win”,“sjis-win”);
encode=(mbdetectencoding(encode = (mb_detect_encoding(text, $charsets));
return $encode;
}
public static function strlen()
{
static func=null;if(isnull(func = null; if (is_null(func)) {

$func = ‘strlen’;
if (function_exists(‘mb_strlen’)) {
$func = ‘mb_strlen’;
}else if (function_exists(‘iconv_strlen’)) {
$func = ‘iconv_strlen’;
}

}

$args = func_get_args();
encode=self::detectencoding(encode = self::detect_encoding(args[0]);
$args[1] = $encode;

return call_user_func_array($func, $args);
}
public static function substr()
{
static func=null;if(isnull(func = null; if (is_null(func)) {

$func = ‘substr’;
if (function_exists(‘mb_substr’)) {
$func = ‘mb_substr’;
}else if (function_exists(‘iconv_substr’)) {
$func = ‘iconv_substr’;
}

}
$args = func_get_args();
encode=self::detectencoding(encode = self::detect_encoding(args[0]);

$args[3] = $encode;

return call_user_func_array($func, KaTeX parse error: Expected 'EOF', got '}' at position 8: args); }̲ public static …str, KaTeX parse error: Expected '}', got 'EOF' at end of input: …=false) { if (!offset) offset=0;returnself::ordutf8(offset = 0; return self::ordutf8(str, KaTeX parse error: Expected 'EOF', got '}' at position 10: offset); }̲ private static…string, &$offset)
{
code=ord(substr(code = ord(substr(string, offset,1));if(offset,1)); if (code >= 128) { //otherwise 0xxxxxxx
if ($code < 224) bytesnumber=2;//110xxxxxelseif(bytesnumber = 2; //110xxxxx else if (code < 240) bytesnumber=3;//1110xxxxelseif(bytesnumber = 3; //1110xxxx else if (code < 248) $bytesnumber = 4; //11110xxx
$codetemp = code192(code - 192 - (bytesnumber > 2 ? 32 : 0) - (bytesnumber>3?16:0);for(bytesnumber > 3 ? 16 : 0); for (i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
code2=ord(substr(code2 = ord(substr(string, $offset, 1)) - 128; //10xxxxxx
$codetemp = $codetemp*64 + $code2;
}
$code = $codetemp;
}
offset+=1;if(offset += 1; if (offset >= strlen($string)) $offset = -1;
return KaTeX parse error: Expected 'EOF', got '}' at position 7: code; }̲ public static …a, $b)
{
return (
$a >= 0
? $a >> b:((b : ((a & 0x7fffffff) >> b)(0x40000000>>(b) | (0x40000000 >> (b - 1))
);
}
public static function charAt(a,a,i)
{
return self::substr(a,a,i,1);
}
public static function hex_md5(KaTeX parse error: Expected '}', got 'EOF' at end of input: …r){ return md5(str);
}

public static function toString(str,str,dec=null){
if(dec==16)returndechex(dec==16)return dechex(str);
return (string)$str;
}

public static function reverse(KaTeX parse error: Expected '}', got 'EOF' at end of input: … array_reverse(arr);
}
public static function string_lenght($string) {
return strlen(iconv(‘UTF-8’, ‘UTF-16LE’, $string)) / 2;
}

public static function charCodeAt($str, $index){
//not working!
char=mbsubstr(char = mb_substr(str, index,1,UTF8);if(mbcheckencoding(index, 1, 'UTF-8'); if (mb_check_encoding(char, ‘UTF-8’))
{
ret=mbconvertencoding(ret = mb_convert_encoding(char, ‘UTF-32BE’, ‘UTF-8’);
return hexdec(bin2hex($ret));
} else {
return null;
}
}

}

//一段由js代碼轉換而來的加密算法

<?php class mmb_d{ static $zero = array("0", "00", "000", "0000", "00000", "000000", "0000000", "00000000"); static $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); static public function strReverse($a) { $b='';$c = array(); for ($b = 0, $l = strlen($a); $b < $l; $b++) $c[] = $a{$b}; return implode('',array_reverse($c)); } static public function encrypt($a, $b, $e) { $a1 = static::shuzi($a); $a2 = static::zimu($a); $a = $a2 . $a1; $f=''; $g = array(); for ($f = 0, $l = strlen($a); $f < $l; $f++) $g[] = static::to(js2php::charCodeAt($a,$f), $b); return static::rnd($e ? static::strReverse(implode("",$g)) : implode("",$g), 4); } static public function to($a, $c) { $e = "" . js2php::toString(static::round($a + 88, $c),16); $f = $c - strlen($e); return $f > 0 ? static::$zero{$f - 1} . $e : $e; } static public function round($a, $b) { $c = 1 << 4 * $b; return 0 > $a ? $a % $c + $c : $a % $c; } static public function shuzi($a) { return preg_replace('/[^0-9]+/i', "",$a); } static public function zimu($a) { return preg_replace('/[^a-zA-Z]+/i', "",preg_replace('/https/', "http",strtolower($a))); } static public function rnd($a, $b) { return static::rd($b) . js2php::hex_md5($a) . static::rd(ceil(('0.'.mt_rand()) * 10)); } static public function rd($a) { $res = ""; for ( $i = 0; $i < $a; $i++) { $res .= static::$chars[ceil(('0.'.mt_rand()) * 35)]; } return $res; } } ?>

對應的javascript代碼如下:

發佈了61 篇原創文章 · 獲贊 43 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章