2013-12-09 18 views
1

這是在黑暗中的一點點......但是沒有人有用PHP代碼實現的mysql編碼和解碼函數的實現。我有使用嵌入式mysql函數編碼的數百萬位數據。需要從數據庫調用中取出編碼功能並在php中實現它。我知道這是一種不安全的方法......但字符串非常小,我繼承了它。MYSQL編碼/解碼函數的PHP實現

問候

+0

什麼編碼和解碼功能?有很多...... – Haneev

+0

你的意思是https://dev.mysql.com/doc/refman/5.5/en/encryption-functions.html#function_encode?另外MySQL建議不再使用這些函數。 – Tommassiov

+0

在源代碼中找到http://dev.mysql.com/doc/internals/en/guided-tour.html – 2013-12-09 22:19:48

回答

0

我大約在同一時間有同樣的問題,我寫了基於C#源代碼片段PHP類(ES) - 我知道這晚,但在這裏你去:

<?php 
class rand { 
    private $_seed1; 
    private $_seed2; 
    private $_max_value; 

    public function __construct($seed1,$seed2) { 
    $this->_max_value = 1073741823; // x3fffffff 
    $this->_seed1 = (int)((float)$seed1 % $this->_max_value); 
    $this->_seed2 = (int)((float)$seed2 % $this->_max_value); 
    } // end-function __construct 

    public function my_rnd() { 
    $this->_seed1 = intval(fmod((($this->_seed1 * 3) + $this->_seed2),$this->_max_value)); 
    $this->_seed2 = intval(fmod(($this->_seed1 + $this->_seed2 + 33),$this->_max_value)); 
    return (float)$this->_seed1/$this->_max_value; 
    } // end-function my_rnd 
} // end-class rand 

class MySQLCrypt { 
    private $_decode_buff; 
    private $_encode_buff; 
    private $_rand; 
    private $_shift; 
    private $_rand_org; 

    public function __construct($password='') { 
    $rand_nr = $this->_hash_password($password); 
    $this->_crypt_init($rand_nr); 
    } // end-function __construct 

    private function _crypt_init($rand_nr) { 
    $this->_rand = new rand($rand_nr[0],$rand_nr[1]); 

    for ($i=0; $i<=255; $i++) 
     $this->_decode_buff[$i] = chr($i); 

    for ($i=0; $i<=255; $i++) { 
     $idx = intval($this->_rand->my_rnd() * 255.0); 
     $a = $this->_decode_buff[$idx]; 
     $this->_decode_buff[$idx] = $this->_decode_buff[$i]; 
     $this->_decode_buff[$i] = $a; 
    } // end-for 

    for ($i=0; $i<=255; $i++) 
     $this->_encode_buff[ord($this->_decode_buff[$i])] = chr($i); 

    $this->_shift = 0; 
    $this->_rand_org = clone $this->_rand; 
    } // end-function _crypt_init 

    private function _hash_password($password) { 
    $nr = 1345345333; 
    $nr2 = 305419889; 
    $add = 7; 

    $result = array(); 
    for ($i=0; $i<strlen($password); $i++) { 
     if ($password[$i] == ' ' or $password[$i] == "\t") { 
     } else { 
     $tmp = ord($password[$i]); 

     $nr ^= ((($nr & 63) + $add) * $tmp) + ($nr * 256); 
     $nr = $this->_makeInt($nr); 
     $nr2 += ($nr2 * 256)^$nr; 
     $nr2 = $this->_makeInt($nr2); 

     $add += $tmp; 
     } 
    } // end-for 
    $result[0] = $nr & 2147483647; // 7fffffff 
    $result[1] = $nr2 & 2147483647; // 7fffffff 
    return $result; 
    } // end-function _hash_password 

    private function _makeInt($value) { 
    if ($value >= 2147483648) 
     $result = intval($value - 2147483648); 
    else if ($value < 0) 
     $result = intval($value + 2147483648); 
    else 
     $result = $value; 
    return $result; 
    } // end-function _makeInt 

    public function decode($str,$password=null) { 
    if ($password === null) { 
     $this->_shift = 0; 
     $this->_rand = clone $this->_rand_org; 
    } else 
     $this->__construct($password); 

    $c_str = $str; 

    for ($i=0; $i<strlen($str); $i++) { 
     $this->_shift ^= intval($this->_rand->my_rnd() * 255.0); 
     $idx = ord($c_str[$i])^$this->_shift; 
     $c_str[$i] = $this->_decode_buff[$idx]; 
     $this->_shift ^= ord($c_str[$i]); 
    } // end-for 
    return $c_str; 
    } // end-function decode 

    public function encode($str,$password=null) { 
    if ($password === null) { 
     $this->_shift = 0; 
     $this->_rand = clone $this->_rand_org; 
    } else 
     $this->__construct($password); 

    $c_str = $str; 

    for ($i=0; $i<strlen($str); $i++) { 
     $this->_shift ^= intval($this->_rand->my_rnd() * 255.0); 
     $idx = ord($c_str[$i]); 
     $c_str[$i] = chr(ord($this->_encode_buff[$idx])^$this->_shift); 
     $this->_shift ^= $idx; 
    } // end-for 
    return $c_str; 
    } // end-function encode 
} // end-class MySQLCrypt 

用法:
1)的所有編碼/解碼的呼叫一個密碼:

// instantiate class and set password 
$obj_MySQLCrypt = new MySQLCrypt('somepassword'); 

// e.g. encode/decode using above password 
$str_cipher = $obj_MySQLCrypt->encode('message in a bottle!'); 
$str_plaintext = $obj_MySQLCrypt->decode($str_cipher); 

2)或不同的密碼對每個編碼/解碼呼叫(較慢):

// instantiate without password 
$obj_MySQLCrypt = new MySQLCrypt(); 
$str_cipher1 = $obj_MySQLCrypt->encode('I hope that someone gets my...','password1'); 
$str_cipher2 = $obj_MySQLCrypt->encode('...message in a bottle!','password2'); 

$str_plaintext1 = $obj_MySQLCrypt->decode($str_cipher1,'password1'); 
$str_palintext2 = $obj_MySQLCrypt->decode($str_cipher2,'password2');