0

我写了一个小类来发送带有 url 的私有数据(不能使用 cookie/会话或其他任何东西)。我使用 PHP 的 mcrypt 对其进行加密/解密,并对其进行 base64-en/解码以在 url 中使用。

不幸的是,我仍然不时得到错误的结果。+我注意到,当网址中至少出现 a 时,总是会发生这种情况。我也玩过rawurlencodeand urlencode/ urldecode,但没有成功。我也尝试strtr()了加密数据,但不知何故+仍然出现。有人有想法吗?

这是我的课:

class crypto 
{
    public function __construct()
    {
        $this->iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
        $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND);
        $this->llave = 'da332sdf9'; 
    }

    public function make_crypt($string)
    {   
        $crypt = mcrypt_encrypt(MCRYPT_BLOWFISH, $this->llave, $string, MCRYPT_MODE_ECB, $this->iv);
        $crypt = rawurlencode(base64_encode($crypt));
        $crypt = strtr($crypt, '+/', '-_');     
        return $crypt;
    }

    public function get_crypt($data)
    {   
        $crypt = strtr($crypt, '-_', '+/');     
        $data = base64_decode($data);
        $decrypted = mcrypt_decrypt (MCRYPT_BLOWFISH, $this->llave, $data, MCRYPT_MODE_ECB, $this->iv);
        return $decrypted;
    }
}
4

2 回答 2

1

你是什​​么意思你尝试了“没有成功”的 URL 编码?URL 编码该值确实有效,否则该功能将被破坏。我在我的框架中使用它并且没有错误。

您确定要对加密值进行编码吗?不要对整个 URL 进行编码。

于 2012-04-24T11:37:18.917 回答
1

观察

$crypt = strtr($crypt, '-_', '+/');?? 在哪里crypt定义???

rawurlencode解码在哪里....

我总是喜欢HEX这样我就不必担心url安全字符

示例测试您当前的课程将失败

$crypt = new Crypto ();
echo "<pre>";
for($i = 0; $i < 10; $i ++) {
    $pass = generatePassword ( mt_rand ( 5, 10 ) );
    $test = $crypt->make_crypt ( $pass );
    $output = $crypt->get_crypt ( $test );

    if ($pass == $output) {
        echo " $pass ($test) = $output \n";
    } else {
        var_dump ( $pass, $output );
        echo " $pass ($test) != $output \n";
    }
}

解决方案

class Crypto {

    private $iv_size;
    private $iv;
    private $llave;

    public function __construct() {
        $this->iv_size = mcrypt_get_iv_size ( MCRYPT_BLOWFISH, MCRYPT_MODE_ECB );
        $this->iv = mcrypt_create_iv ( $this->iv_size, MCRYPT_RAND );
        $this->llave = 'da332sdf9';
    }

    public function make_crypt($string) {
        $crypt = mcrypt_encrypt ( MCRYPT_BLOWFISH, $this->llave, $string, MCRYPT_MODE_ECB, $this->iv );
        return bin2hex ( $crypt );
    }

    public function get_crypt($data) {
        $data = pack ( "H*", $data );
        $decrypted = mcrypt_decrypt ( MCRYPT_BLOWFISH, $this->llave, $data, MCRYPT_MODE_ECB, $this->iv );
        return trim ( $decrypted );
    }
}

解决方案输出

 tXHhC8fk4 (b929695d39555523348051a72d15baaf) = tXHhC8fk4 
 drKH9 (909994926fe5cd30) = drKH9 
 mNwh6K (10af1bb381338943) = mNwh6K 
 CJZvqwGX (aa705c290759b18d) = CJZvqwGX 
 Jt4W7j (bc7ee842041b9860) = Jt4W7j 
 tgCHXyPvm (9f46b74ef59ee70da1dda30b3e52fe92) = tgCHXyPvm 
 LYxhVj (9e2079cff9d54007) = LYxhVj 
 kR8WLwh3T (3e4606d65defc74f3c82af5fb095f41d) = kR8WLwh3T 
 Z8NqWM6RHj (1d9eea2358674d78cc43e024cba5ba48) = Z8NqWM6RHj 
 TtqRf7M (09ef38114bb729c4) = TtqRf7M 

用于生成数据的函数

function generatePassword($length = 8) {
    $password = "";
    $possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";
    $maxlength = strlen ( $possible );
    if ($length > $maxlength) {
        $length = $maxlength;
    }
    $i = 0;
    while ( $i < $length ) {
        $char = substr ( $possible, mt_rand ( 0, $maxlength - 1 ), 1 );
        if (! strstr ( $password, $char )) {
            $password .= $char;
            $i ++;
        }

    }
    return $password;

}
于 2012-04-24T12:14:04.843 回答