2013-05-06 22 views
0

我用公鑰加密,然後編碼的字符串,就像200個字符。

SENDER: 
$pubkey = file_get_contents('http://localhost/test/key.pub'); 
openssl_public_encrypt('hello world', $encrypted, $pubkey); 
$first = $encrypted; 
$url = 'http://localhost/demo_project/bank/?secure='.urlencode($first); 
echo file_get_contents($url); 

RECIEVER: 
var_dump($_GET); 

輸出:

array(1) { ["secure"]=> string(0) "" } 

安全是secure=Qq%B8%143%F5%D1%15%C4%18%95g%D6%D0%2F%2CH%25%F8%A8%17%EF%2Bl%80%3Bc%9E%F2%9A%FB%CF3%EDj%B7%26%0F%A0%5E%1DM%AB%07%1Db%0F%C3%9E%A1%FF%82%7D%E50%15Vc%08t%0F%07%0Ag

最奇怪的事是,如果secure=Qq%B,一切工作正常,但如果secure=Qq%B8,我們有一個問題。

我也試過用捲曲像here,但它也沒有做任何好處。有人會指出我的錯誤並提出解決方案嗎?

回答

0

base64編碼的目的是使通過不在8位乾淨 傳輸層的二進制數據生存運輸。

您必須對openssl_public_encrypt()的二進制輸出數據進行編碼。

發件人:

$pubkey = file_get_contents('http://localhost/test/key.pub'); 
openssl_public_encrypt('hello world', $encrypted, $pubkey); 
$first = base64_encode($encrypted); 
$url = 'http://localhost/demo_project/bank/?secure='.urlencode($first); 
echo file_get_contents($url); 

接收機:

var_dump($_GET); 
$secure = base64_decode($_GET['secure']); 

我會使用POST

$url = 'http://localhost/demo_project/bank/'; 
$postdata = array("secure" => $first); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata)); 
echo $response = curl_exec($ch); 

curl_close($ch);