簡體   English   中英

openssl_decrypt圖片未顯示

[英]openssl_decrypt images not showing

我有一種形式,將它們插入數據庫中。 我使用open ssl加密圖像,然后將其存儲在數據庫中。

我還解密並顯示它們。 從數據庫中取出並解密后,圖像未顯示。

我似乎無法找出未顯示圖像的原因。 從我可以看到它們被正確插入數據庫中

  <?php

if(isset($_POST["action"]))
{
 $connect = mysqli_connect("","","","");


  $cipher = "aes-128-cbc";
        $ivlen = openssl_cipher_iv_length($cipher);

        $key = openssl_random_pseudo_bytes(128);


 if($_POST["action"] == "fetch")
 {
  $query = "SELECT * FROM tbl_images where r_id = '".$_POST["r_id"]."' ORDER BY id DESC";
  $result = mysqli_query($connect, $query);

  $output = '
   <table class="table table-bordered table-striped">  
    <tr>
     <th width="10%">ID</th>
     <th width="70%">Image</th>
     <th width="10%">Remove</th>
    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {

      $newciphertext = $row["name"];
$newiv = $row["iv"];


 $img = openssl_decrypt($newciphertext, $cipher, $key, $options=0, $newiv);

   $output .= '
    <tr>
     <td>'.$row["id"].'</td>
     <td>
      <a target="_blank" href="#" onClick="enlarge(this)""><img src="data:image/jpeg;base64,'.base64_encode($img).'" class="img-thumbnail"/></a>
     </td>
     <td><button type="button" name="delete" class="btn btn-danger bt-xs delete" id="'.$row["id"].'">Remove</button></td>
    </tr>
   ';
  }
  $output .= '</table>';
  echo $output;
 }

 if($_POST["action"] == "insert")
 {

  $file = file_get_contents($_FILES["image"]["tmp_name"]);

$iv = openssl_random_pseudo_bytes($ivlen);
        $ciphertext = openssl_encrypt($file, $cipher, $key, $options=0, $iv);

  $query = "INSERT INTO tbl_images(name, r_id, iv) VALUES ('\"" . addslashes($ciphertext) ."\"', '".$_POST["r_id"]."', '$iv')";
  if(mysqli_query($connect, $query))
  {
   echo 'Image Inserted into Database';
  }
 }
 if($_POST["action"] == "delete")
 {
  $query = "DELETE FROM tbl_images WHERE id = '".$_POST["image_id"]."'";
  if(mysqli_query($connect, $query))
  {
   echo 'Image Deleted from Database';
  }
 }
}
?>

每次訪問該頁面時,都會計算出一個新的$iv ,該值對於加密部分來說很好,但是您需要相同的$iv來解密圖像。 因此,當您嘗試解密圖像時,您將使用錯誤的$iv ,解密將失敗。 您應該將$iv保存在數據庫中的新字段中,或者與加密的映像並置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM