一段javascript转PHP的encrypt加密算法

这段博客内容展示了如何将JavaScript的加密算法转换为PHP代码,包括detect_encoding、strlen、substr等函数的转换,并提供了一个名为mmb_d的加密类,实现了加密方法。此外,还给出了JavaScript的MD5相关函数,包括hex_md5、b64_md5、str_md5等。
摘要由CSDN通过智能技术生成

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 = (mb_detect_encoding($text, $charsets));
return $encode;
}
public static function strlen()
{
static $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::detect_encoding($args[0]);
$args[1] = $encode;

return call_user_func_array($func, $args);
}
public static function substr()
{
static $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::detect_encoding($args[0]);

$args[3] = $encode;

return call_user_func_array($func, $args);
}
public static function ord($str, $offset=false)
{
if (!$offset) $offset = 0;
return self::ordutf8($str, $offset);
}
private static function ordutf8($string, &$offset)
{
$code = ord(substr($string, $offset,1));
if ($code >= 128) {        //otherwise 0xxxxxxx
if ($code < 224) $bytesnumber = 2;                //110xxxxx
else if ($code < 240) $bytesnumber = 3;        //1110xxxx
else if ($code < 248) $bytesnumber = 4;    //11110xxx
$codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
for ($i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
$code2 = ord(substr($string, $offset, 1)) - 128;        //10xxxxxx
$codetemp = $codetemp*64 + $code2;
}
$code = $codetemp;
}
$offset += 1;
if ($offset >= strlen($string)) $offset = -1;
return $code;
}
public static function unsignedRightShift($a, $b)
{
return (
$a >= 0
? $a >> $b
: (($a & 0x7fffffff) >> $b) | (0x40000000 >> ($b - 1))
);
}
public static function charAt($a,$i)
{
return self::substr($a,$i,1);
}
public static function hex_md5($str){
return md5($str);
}

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

public static function reverse($arr){
return 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 = mb_substr($str, $index, 1, 'UTF-8');
if (mb_check_encoding($char, 'UTF-8'))
{
$ret 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值