I'm running the same mysqli insert query using the exact same parameters. It is successful about 1/3 of the time, and I cannot figure out what is going wrong.
<?php
//...
$decrypted_session_key = 'unavailable'; // initialize
$res = openssl_get_privatekey($priv_key, $passphrase);
$result = openssl_private_decrypt($encrypted_session_key, $decrypted_session_key, $res, OPENSSL_PKCS1_OAEP_PADDING);
if ($decrypted_session_key == 'unavailable') {
mysqli_close($link);
echo json_encode(array('result' => 'failed', 'message' => 'failed to decrypt the session key'));
die();
}
if (!$decrypted_session_key) {
echo json_encode(array('result'=>'failed', 'message'=>'decrypted session key has failed'));
die();
}
$updated_at = date("Y-m-d H:i:s");
// save this record to the database
$result = mysqli_query($link, "INSERT INTO Session_Keys (session_id, session_key, iv, updated_at)
VALUES ($session_id, '$decrypted_session_key', '$iv', '$updated_at')");
if (!$result) {
$param_check = $session_id . " " . base64_encode($iv) . " " . base64_encode($decrypted_session_key) . " " . $updated_at;
echo json_encode(array('result'=>'failed', 'message'=>$param_check));
die();
}
// ...
}
Whenever this fails, I get the last echo statement returned. My suspicion was that the php decryption routine was failing, but it's not. All of the parameters are perfect, including the decryption value. Is the problem that my insert statement is wrong? I've tried various combination of quoting the fields, but nothing consistent results.
The table structure is like this:
'session_id' int(11)
'session_key' tinyblob
'iv' tinyblob
'updated_at' datetime
I don't understand why my results are so inconsistent. If it fails, why not fail every time? If it works, why doesn't it work every time? Very confused. Any help is appreciated. Thanks!