首页 > 解决方案 > 通过 PHP REST API 检索加密的 JSON

问题描述

在本地主机中,我正在尝试开发和测试我的 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;

有人可以帮我理解什么是错的,如果这是正确的方法吗?

标签: phprestencryptionphp-curl

解决方案


最后我以这种方式得到了结果:

发送请求的文件

//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;

推荐阅读