PHP实现MYSQL编码/解码功能 [英] PHP implementation of MYSQL encode/decode functions

查看:62
本文介绍了PHP实现MYSQL编码/解码功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是黑暗中的镜头...但是没有人能实现以PHP代码实现的mysql编码和解码功能.我有使用嵌入式mysql函数编码的数百万位数据.需要从数据库调用中删除编码功能,并在php中实现.我知道这是一种不安全的方法...但是字符串很小,我继承了它.

This is a bit of a shot in the dark...but does anyone have an implementation of the mysql encode and decode functions implemented in PHP code. I have a several million bits of data encoded using the embedded mysql functions. Need to take the encode functionality out of the database calls and implement it in php. I know it is an insecure method...but the strings are very small and I inherited it.

致谢

推荐答案

我几乎同时遇到了相同的问题,我基于C#源代码片段编写了一个php类-我知道已经很晚了,但是在这里前往:

I had the same problem at about the same time, I wrote a php class(es) based on a C# source snippet - I know it's late but here you go:

<?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)所有编码/解码调用使用一个密码:

Usage:
1) one password for all encode/decode calls:

// 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)或每个编码/解码调用的密码不同(较慢):

2) or different password for each encode/decode call (slower):

// 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');

这篇关于PHP实现MYSQL编码/解码功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆