MYSQL编码/解码函数的PHP实现
这有点像是瞎猜……但是有没有人有用PHP代码实现的mysql编码和解码功能的实现。我使用嵌入式mysql函数对数百万位数据进行编码。需要从数据库调用中提取编码功能,并在php中实现它。我知道这是一个不安全的方法…但是字符串非常小,我继承了它MYSQL编码/解码函数的PHP实现,php,mysql,encryption,Php,Mysql,Encryption,这有点像是瞎猜……但是有没有人有用PHP代码实现的mysql编码和解码功能的实现。我使用嵌入式mysql函数对数百万位数据进行编码。需要从数据库调用中提取编码功能,并在php中实现它。我知道这是一个不安全的方法…但是字符串非常小,我继承了它 关于大约在同一时间,我也遇到了同样的问题,我根据一个C#源代码片段编写了一个php类-我知道已经很晚了,但现在开始: <?php class rand { private $_seed1; private $_seed2; private
关于大约在同一时间,我也遇到了同样的问题,我根据一个C#源代码片段编写了一个php类-我知道已经很晚了,但现在开始:
<?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
2) 或每个编码/解码呼叫的不同密码(较慢):什么是编码和解码功能?有很多…你是说?MySQL还建议不再使用这些函数。在源代码中找到它。是的……这些是正确的函数。我知道它们已不再使用,但问题在于开发一个模型/控制器分离更好的系统。由于数据库负责编码的方式,我们无法实现我们正在研究的一些缓存机制
// 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);
// 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');