[英]Retrieve encrypted JSON via PHP REST API
在本地主機中,我正在嘗試開發和測試我的 REST API。 我有這個代碼發送一個 JSON 加密到 REST API
//The url i wish to send the POST request to
$url = "http://localhost/api/2.php";
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
// Key
$encryption_key = "43274689933404c4bd47190b395f5e3a2c668fcca603c40ceb074c970047402d";
$iv = "274f5f54eff39aee1e4d2c614ccd99c9";
$method = "AES-256-CBC";
//The data to send via POST
$data = [
'username' => "RERERE",
'password' => "bbbb"
];
// Encrypted data
$encrypted = base64_encode(openssl_encrypt($data, $method, $encryption_key, 0, $iv));
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_POSTFIELDS, $encrypted);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
echo $result;
現在在 2.php 文件中我無法檢索內容,我在下面使用類似這樣的東西,但我得到了 NULL
// Key
$encryption_key = "43274689933404c4bd47190b395f5e3a2c668fcca603c40ceb074c970047402d";
$iv = "274f5f54eff39aee1e4d2c614ccd99c9";
$method = "AES-256-CBC";
$json = file_get_contents('php://input',true);
$array = json_decode($json);
$decrypted = openssl_decrypt(base64_decode($array), $method, $encryption_key, 0, $iv);
echo $decrypted;
有人可以幫我理解什么是錯的,如果這是正確的方法嗎?
最后我以這種方式得到了結果:
發送請求的文件
//The url you wish to send the POST request to
$url = "http://localhost/api/2.php";
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
);
$username = "aaaaaa";
$password = "bbbbbb";
// Key
$encryption_key = "43274689933404c4bd47190b395f5e3a2c668fcca603c40ceb074c970047402d";
$iv = "274f5f54eff39aee1e4d2c614ccd99c9";
$method = "AES-256-CBC";
$usernameEncrypted = base64_encode(openssl_encrypt($username, $method, $encryption_key, 0, $iv));
$passwordEncrypted = base64_encode(openssl_encrypt($password, $method, $encryption_key, 0, $iv));
//The data to send via POST
$fields = [
'username' => $usernameEncrypted,
'password' => $passwordEncrypted
];
$fields = json_encode($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
//execute post
$result = curl_exec($ch);
echo $result;
以及接收請求並發送 echo() 的文件
$encryption_key = "43274689933404c4bd47190b395f5e3a2c668fcca603c40ceb074c970047402d";
$iv = "274f5f54eff39aee1e4d2c614ccd99c9";
$method = "AES-256-CBC";
$datiInArray = json_decode(file_get_contents("php://input"),true);
$usernameCrypted = $datiInArray["username"];
$passwordCrypted = $datiInArray["password"];
$username = openssl_decrypt(base64_decode($usernameCrypted), $method, $encryption_key, 0, $iv);
$password = openssl_decrypt(base64_decode($passwordCrypted), $method, $encryption_key, 0, $iv);
// Here echo the result
echo $username."<br>".$password;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.