$dataMsg = json_decode($jsonForDecode, true);
foreach($dataMsg["messages_dike"] as $itemRes) {
$ciphertext = $itemRes["message_text"];
//.......................
$c = base64_decode(json_encode($ciphertext,JSON_UNESCAPED_UNICODE));
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $cipher, ENCRYPTION_KEY, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary=true);
//.......................
$itemRes["message_text"] = $plaintext;
if (hash_equals($hmac, $calcmac))
{
print json_encode($dataMsg,JSON_UNESCAPED_UNICODE);
print json_encode($itemRes,JSON_UNESCAPED_UNICODE);
} // Вывел оба, чтобы сравнить их
$itemRes["message_text"] = $plaintext;
if (hash_equals($hmac, $calcmac))
{
print json_encode($itemRes,JSON_UNESCAPED_UNICODE);
}
$dataMsg = json_decode($jsonForDecode, true);
foreach($dataMsg["messages_dike"] as $itemRes) {
$ciphertext = $itemRes["message_text"];
$c = base64_decode(json_encode($ciphertext,JSON_UNESCAPED_UNICODE));
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $cipher, ENCRYPTION_KEY, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary=true);
if (hash_equals($hmac, $calcmac))
{
$replacements = array("message_text" => $plaintext);
$dataMsg = array_replace($dataMsg, $replacements);
}
}
print json_encode($dataMsg,JSON_UNESCAPED_UNICODE);
$dataMsg = json_decode($jsonForDecode, true);
foreach($dataMsg["messages_dike"] as $itemRes) {
$ciphertext = $itemRes["message_text"];
$c = base64_decode(json_encode($ciphertext,JSON_UNESCAPED_UNICODE));
$ivlen = openssl_cipher_iv_length($cipher="AES-128-CBC");
$iv = substr($c, 0, $ivlen);
$hmac = substr($c, $ivlen, $sha2len=32);
$ciphertext_raw = substr($c, $ivlen+$sha2len);
$plaintext = openssl_decrypt($ciphertext_raw, $cipher, ENCRYPTION_KEY, $options=OPENSSL_RAW_DATA, $iv);
$calcmac = hash_hmac('sha256', $ciphertext_raw, ENCRYPTION_KEY, $as_binary=true);
if (hash_equals($hmac, $calcmac))
{
$replacements = array("message_text" => $plaintext);
$dataMsg = array_replace($dataMsg, $replacements);
}
}
$who_sent = $_GET['who_sent'];
$who_get = $_GET['who_get'];
$db = mysqli_select_db($connect, DB_NAME) or die(mysqli_error($connect)); // true or false
mysqli_set_charset($connect, "utf8mb4");
$sql = "SELECT * FROM messages_dike WHERE who_sent = '$who_sent' AND who_get = '$who_get' OR who_sent = '$who_get' AND who_get = '$who_sent' ORDER BY `message_datetime`";
$result = mysqli_query($connect, $sql);
if (mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$lineSelect['messages_dike'][] = $row;
}
print json_encode($lineSelect,JSON_UNESCAPED_UNICODE);
} else {
print json_encode($error, JSON_UNESCAPED_UNICODE);
}