[ Content | Sidebar ]

Archives for 三月, 2010

php整型溢出导致的小问题~

今天把一直本地测试的php程序放到外网的机器跑(64位系统),没想到一个纯算数值的算法竟然得到不一样的结果,最后锁定在一个求模的操作上:$i % 4, $i在本地和server上打印出来都是434807526976, 但是求出来的结果却不一样~

原因是:
php的整型最大值为2147483647(32位系统上), 当数值赋值超过这个最大值时,变量便自动成为浮点型. 所以虽然打印出来一样,但本地打印的是一个是浮点型的$i,而server上是64位系统,整型最大值达到9223372036854775807  …. $i依然是一个整型.

取模运算的结果不一样是因为php在对数值取模运算时会把浮点数转换为整型再取模(这点就不如js了,js是可以对浮点取模运算地~)

所以本地32位系统运行的其实是: ((int) $i) % 4 即 512559680 % 4

解决方法 : 最后为了保持与其他平台的一致,强制在64系统也”溢出” : ($i & 0xFFFFFFFF) % 4

————————————————

php也是一个比较无厘头的东西~

php实现AES/ECB/PKCS5Padding加密

基于一哥们代码的修改版:http://www.codigg.com/2010/01/php-aes-java-encrypt-decrypt/

class CryptAES
{
    protected $cipher     = MCRYPT_RIJNDAEL_128;
    protected $mode       = MCRYPT_MODE_ECB;
    protected $pad_method = NULL;
    protected $secret_key = '';
    protected $iv         = '';
 
 
    public function set_cipher($cipher)
    {
        $this->cipher = $cipher; 
    }
 
    public function set_mode($mode)
    {
        $this->mode = $mode;
    }
 
    public function set_iv($iv)
    {
        $this->iv = $iv;
    }
 
    public function set_key($key)
    {
        $this->secret_key = $key;
    }
 
    public function require_pkcs5()
    {
        $this->pad_method = 'pkcs5';
    }
 
    protected function pad_or_unpad($str, $ext)
    {
        if ( is_null($this->pad_method) )
        {
            return $str;
        }
        else 
        {
            $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
            if ( is_callable($func_name) )
            {
                $size = mcrypt_get_block_size($this->cipher, $this->mode);
                return call_user_func($func_name, $str, $size);
            }
        }
 
        return $str; 
    }
 
    protected function pad($str)
    {
        return $this->pad_or_unpad($str, ''); 
    }
 
    protected function unpad($str)
    {
        return $this->pad_or_unpad($str, 'un'); 
    }
 
 
    public function encrypt($str)
    {
        $str = $this->pad($str);
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
 
        if ( empty($this->iv) )
        {
            $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }
        else
        {
            $iv = $this->iv;
        }
 
        mcrypt_generic_init($td, $this->secret_key, $iv);
        $cyper_text = mcrypt_generic($td, $str);
        $rt = bin2hex($cyper_text);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
 
        return $rt;
    }
 
 
    public function decrypt($str){
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
 
        if ( empty($this->iv) )
        {
            $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }
        else
        {
            $iv = $this->iv;
        }
 
        mcrypt_generic_init($td, $this->secret_key, $iv);
        $decrypted_text = mdecrypt_generic($td, self::hex2bin($str));
        $rt = $decrypted_text;
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
 
        return $this->unpad($rt);
    }
 
    public static function hex2bin($hexdata) {
        $bindata = '';
        $length = strlen($hexdata); 
        for ($i=0; $i < $length; $i += 2)
        {
            $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
        }
        return $bindata;
    }
 
    public static function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }
 
    public static function pkcs5_unpad($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);
    }
}
Copy Code 

主要改进就是支持pkcs5补齐了,使用方法如下~

$aes = new CryptAES();
$aes->set_key('1234567890123456');
$aes->require_pkcs5();
$rt = $aes->encrypt('abcd');
echo $rt . '<br/>';
echo $aes->decrypt($rt) . '<br/>';
Copy Code 

二进制和位运算的一些文章

http://www.matrix67.com/blog/archives/263 //重点看它后续的二三四篇

http://www.cnblogs.com/911/archive/2008/05/20/1203477.html

http://www.blueidea.com/tech/program/2006/3153.asp //这篇文章讲的不是很明白,搜权限系统设计

计算机文化基础开头就讲二进制…, 但是看过上面的文章之后会对二进制有更深的理解

一个php应用最开始一般会做的事

OK,php回顾之旅开始~

  • 检查PHP版本,一般发行版软件会这么做
    //wordpress
    if ( version_compare( '4.3', phpversion(), '>' ) ) {
    //MediaWiki
    # Check for PHP 5
    if ( !function_exists( 'version_compare' )
    	|| version_compare( phpversion(), '5.0.0' ) < 0
    
  • 检查对性能或者安全有影响的配置项
    //wordpress 尝试设置内存限制
    @ini_set('memory_limit', WP_MEMORY_LIMIT);
    
    //MediaWiki
    if ( ini_get( 'register_globals' ) ) {
    //....如果自动全局变量下面将检查是否存在有害的参数
    
    @ini_set( 'allow_url_fopen', 0 ); # For security
    
  • 定义有用的常量
    //wordpress
    //定义根目录
    define( 'ABSPATH', dirname(__FILE__) . '/' );
    //定义语言, 有时会也会根据用户的语言设置初始化
    define ('WPLANG', 'zh_CN');
    
  • 针对不同平台,不同httpd软件的兼容代码,一般发行版软件会做
    //wordpress,针对iis的兼容代码
    // IIS Mod-Rewrite
    	if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) {
    		$_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_ORIGINAL_URL'];
    	}
    
  • 初始化用于debug或者profile的函数,强大与否并不重要,但是没有是灰常不专业的...
    //wordpress
    if ( defined('WP_DEBUG') && WP_DEBUG ) {
    ....
    
  • 加载通用的库文件或者框架
    require (ABSPATH . WPINC . '/functions.php');
    

重新开始

从下周开始,我将变成一名phper,没错,我辞去了现在的前端js工作.
日后的blog将更多的记录一些低级的php和数学或者算法问题,希望两年之后再看这些问题,我会说我当时好弱啊~