我写了一个小班,用url发送私人数据(不能使用cookies / sessions或其他任何东西)。我使用PHP的mcrypt加密/解密它,并且还使用base64-en /解码它以在url中使用。
不幸的是,我最后仍然会得到错误的结果。我注意到,当网址中至少出现+
时,总会发生这种情况。我还使用了rawurlencode
和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;
}
}
答案 0 :(得分:1)
你是说你尝试过URL编码'没有成功'是什么意思?编码该值的URL确实有效,否则该函数将被破坏。我在我的框架中使用它并且没有错误。
您确定要对加密值进行编码吗?不要对整个网址进行编码。
答案 1 :(得分: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;
}