2013-02-14 44 views
0

我有mysite缓存文件,我想从远程获取所有这些文件。我在我的网站上写了一个小应用程序,用查询字符串回显加密的文本。从远程获取文本确定,但文本有一些不好的字符或东西是错误的。 我的远程代码:请求url并获取加密文本

$req = file_get_contents($website.'Cache.php?cacheid='.$filename.'&action=getContent'); 
     $req = trim($req); 
     $req = str_replace (array("\r\n", "\n", "\r"), '', $req); 

     $decryptedText = decrypt(trim($req),'mypass') ; 

    array_push($fileNameTexts,'<div style="color:red;">'.$filename.'</div><div>'.$decryptedText.'</div>'); 
} 
$template->data['decryptedCaches'] = $fileNameTexts; 
} 


function decrypt($encrypted, $password, $salt='mysalt') { 

    **file_put_contents(DIR_SYSTEM.'test'.'.txt',$encrypted);** 

    $key = hash('SHA256', $salt . $password, true); 
    $iv = base64_decode(substr($encrypted, 0, 22) . '=='); 
    $encrypted = substr($encrypted, 22); 
    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4"); 
    $hash = substr($decrypted, -32); 
    $decrypted = substr($decrypted, 0, -32); 
    if (md5($decrypted) != $hash) return false; 
    return $decrypted; 

}  

file_put_contents保存正确的数据,但不能退货解密的真实数据。

当我尝试

$decryptedText = decrypt(trim('kjsfkdsjflkdsflksdjfsl'),'mypass') ; 

它运行正确。我尝试修剪和str_replace一些字符,但它does not work.Has返回数据请求任何坏字符?什么是问题?

回答

0

你必须转换所有坏字符,但base64不起作用。

解决方案:以十六进制格式转换您的字符串。 而不是base64_encode使用bin2hex($yourvalue)和解密这与pack("H*" ,$string);

我希望我的回答可以帮助你。