gpt4 book ai didi

MYSQL编码/解码函数的PHP实现

转载 作者:行者123 更新时间:2023-11-30 23:09:25 26 4
gpt4 key购买 nike

这有点摸不着头脑……但是有没有人有用 PHP 代码实现的 mysql 编码和解码功能的实现。我有数百万位使用嵌入式 mysql 函数编码的数据。需要从数据库调用中取出编码功能并在 php 中实现它。我知道这是一种不安全的方法...但是字符串非常小,我继承了它。

问候

最佳答案

我几乎在同一时间遇到了同样的问题,我基于 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

用法:
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');

关于MYSQL编码/解码函数的PHP实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20482004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com