PHP字符编码的问题,但只与字符T,至少这是我的猜测
我在不同的地方发布了这个类,但现在发现了一个bug。 它已经变得非常流行,所以我想知道是否有人能帮我解决这个问题。 我已经研究了好几个小时了,我发现99%的问题都是大写字母T和小写字母T的问题。 我尝试了不同的编码,t的字符变为不同的字符。但它总是像t的接缝一样 课程PHP字符编码的问题,但只与字符T,至少这是我的猜测,php,encryption,encoding,Php,Encryption,Encoding,我在不同的地方发布了这个类,但现在发现了一个bug。 它已经变得非常流行,所以我想知道是否有人能帮我解决这个问题。 我已经研究了好几个小时了,我发现99%的问题都是大写字母T和小写字母T的问题。 我尝试了不同的编码,t的字符变为不同的字符。但它总是像t的接缝一样 课程 <?php /** * An open source PHP library collection * * @category RoboTamer * @author Dennis T Kapla
<?php
/**
* An open source PHP library collection
*
* @category RoboTamer
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://www.RoboTamer.com/license.html
* @link http://www.RoboTamer.com
**/
/**
* RTCrypt
*
* RTCrypt allows for encryption and decryption on the fly using
* a simple but effective method.
*
* RTCrypt does not require mcrypt, mhash or any other PHP extension,
* it uses only PHP.
*
* @category RoboTamer
* @package RTCrypt
* @author Dennis T Kaplan
* @copyright Copyright (c) 2011, Dennis T Kaplan
* @license http://www.RoboTamer.com/license.html
* @link http://www.RoboTamer.com
**/
class RTCrypt
{
const streight = '012345679ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
private $scramble1 = NULL;
private $scramble2 = NULL;
public function __construct()
{
$this->setScramble1();
}
public function __destruct(){}
public function getStreight()
{
return self::streight;
}
public function getScramble1()
{
return implode($this->scramble1);
}
public function getScramble2()
{
return implode($this->scramble2);
}
/**
* Set the characters you like to replace
*
* @access private
* @param string $str
*/
private function setScramble1()
{
$this->scramble1 = str_split(self::streight);
}
/**
* This is your private key.
* You can generate a random private key based on scramble1 via
* the randomizeString($scramble1) function.
*
* @access public
* @param string $str
* @return bool TRUE
*/
public function setScramble2($str=NULL)
{
if($str===NULL){
trigger_error('No key, use genKey($str)', E_USER_ERROR );
die;
}
$this->scramble2 = str_split($str);
return TRUE;
}
/**
* This will encrypt your data
*
* @access public
* @param string $str
* @return string encrypt data
*/
public function encrypt($str)
{
if($this->scramble2 === NULL) $this->setScramble2();
$str = base64_encode($str);
$len = strlen($str);
$newstr='';
for($i=0; $i < $len;$i++){
$r = substr($str, -1);
$str = substr($str, 0, -1);
$an = array_search($r,$this->scramble1);
if($an > 0){
$newstr .= $this->scramble2[$an];
}else{
$newstr .= $r;
}
}
return $newstr;
}
/**
* This will decrypt a Crypted string back to the original data
*
* @access public
* @param string $str
* @return string
*/
public function decrypt($str)
{
if($this->scramble2 === NULL) $this->setScramble2();
$len = strlen($str);
$newstr='';
for($i=0; $i < $len;$i++){
$r = substr($str, -1);
$str = substr($str, 0, -1);
$an = array_search($r,$this->scramble2);
if($an > 0){
$newstr .= $this->scramble1[$an];
}else{
$newstr .= $r;
}
}
$str = base64_decode($newstr);
return $str;
}
/**
* Generates your private key.
* You would use it to set scramble2
* Keep it save!
*
* @access public
* @return string
*/
public function genKey()
{
$array = str_split(self::streight);
shuffle($array);
return implode($array);
}
}
?>
RTCrypt之前的字符串:
/**
* encode, decode and also serialize when nessesery.
* Works with anything that php can serialize.
* string, array, etc.
*
* @category RoboTaMeR
* @package Strsafe
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://RoboTamer.com/license.html
* @link http://RoboTamer.com
* @todo combine this with RTCrypt to one class
*/
/**
* encode, decode and also serialize when nessesery.
* Works wich anyching chac php can serialize.
* string, array, etc.
*
* @category RoboTaMeR
* @package Strsafe
* @auchor Dennis T Kaplan
* @copyrighc Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license hctp://RoboTamer.com/license.html
* @link hctp://RoboTamer.com
* @codo combine chis with RTCrypt co one class
*/
RTCrypt之后的字符串:
/**
* encode, decode and also serialize when nessesery.
* Works with anything that php can serialize.
* string, array, etc.
*
* @category RoboTaMeR
* @package Strsafe
* @author Dennis T Kaplan
* @copyright Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license http://RoboTamer.com/license.html
* @link http://RoboTamer.com
* @todo combine this with RTCrypt to one class
*/
/**
* encode, decode and also serialize when nessesery.
* Works wich anyching chac php can serialize.
* string, array, etc.
*
* @category RoboTaMeR
* @package Strsafe
* @auchor Dennis T Kaplan
* @copyrighc Copyright (c) 2008 - 2011, Dennis T Kaplan
* @license hctp://RoboTamer.com/license.html
* @link hctp://RoboTamer.com
* @codo combine chis with RTCrypt co one class
*/
我唯一能想到的是:
if($an > 0){
索引0上可能有一个键。您应该使用以下方法检查FALSE
:
if ($an !== FALSE)
我唯一能想到的是:
if($an > 0){
索引0上可能有一个键。您应该使用以下方法检查FALSE
:
if ($an !== FALSE)
即使修复了这个bug,您的代码也是完全不安全的。我一眼就能看出三个缺陷:
+
,/
,8
(看起来你只是把这个忘在了字符串中)和=
。=
的标识映射是可以接受的,因为它只是字符串结尾标记,但其他三个是安全缺陷shuffle
创建私钥shuffle
不是为加密目的而设计的,因此可能使用弱随机数生成器。如果它映射到只有32位种子的c RNG,我不会感到惊讶。这意味着只有40亿个不同的密钥,这对于暴力来说是微不足道的
即使修复了这个bug,您的代码也是完全不安全的。我一眼就能看出三个缺陷:
+
,/
,8
(看起来你只是把这个忘在了字符串中)和=
。=
的标识映射是可以接受的,因为它只是字符串结尾标记,但其他三个是安全缺陷shuffle
创建私钥shuffle
不是为加密目的而设计的,因此可能使用弱随机数生成器。如果它映射到只有32位种子的c RNG,我不会感到惊讶。这意味着只有40亿个不同的密钥,这对于暴力来说是微不足道的
“简单但有效”的委婉语表示“完全不安全”的委婉语“简单但有效”的委婉语表示“完全不安全”,我用它来表示两台服务器之间的简单通信,主要是数据交换。不是为了银行或间谍!但是,感谢您提供的提示,我将在修复错误后立即提供这些提示。当AES库在几乎所有编程语言中都可用时,我认为没有理由使用自制加密。我使用它在两台服务器之间进行简单通信,主要是数据交换。不是为了银行或间谍!但是谢谢你的提示,我会尽快找到他们的错误修复。我认为没有理由永远使用自制加密时,AES库在几乎所有的编程语言可用。