php - 通过 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;
有人可以帮我理解什么是错的,如果这是正确的方法吗?
解决方案
最后我以这种方式得到了结果:
发送请求的文件
//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;
推荐阅读
- javascript - 使用passport-local-mongoose更改密码时如何自动登录
- haskell - 要求代码中定义的特定 Haskell 类型无效
- c# - LINQ 表达式 - 使用 .Any in s Select 无法翻译
- javascript - 使用 javascript 将 2 个连接的集合插入到新集合 MongoDB
- google-apps-script - 如何检查值是否存在于谷歌电子表格中或不使用应用程序脚本
- laravel - 如何在表单中添加旧数据以选择输入(由 vue.js 循环)
- ios - UICollectionViewCompositionalLayout:以小数组宽度控制组中的项目数
- python - python字符串中的逻辑运算符
- rust - Piston-2DGraphics(Rust)中的文本换行和文本背景颜色?
- pyspark - 将 Parquet 文件作为 PySpark 中 Dataframe 的输出写入时,如何在 HDFS 中创建元数据文件?