dpca4790
2017-09-25 02:47
浏览 210
已采纳

使用PHP加密图像以便在MySQL BLOB中存储然后解密和打印

I'm trying to take an uploaded image, encrypt the image, store it in MySQL, then decrypt it for display when an authorized person requests to see it.

Here's how I'm currently encrypting:

$image = addslashes(file_get_contents($_FILES['users_image']['tmp_name']));
$enc_image = encrypt($image, "long secret random key");

Then I store the $enc_image in a MySQL BLOB field. When I try to decrypt it and print it goes like so:

$img = decrypt($rec['file'], "long secret random key");
echo '<img src="data:image/jpeg;base64,'.base64_encode($img).'"/>';

I'm using this code from this Stackoverflow answer, and I'm seeing the decrypted base-64 text, in my output, but it doesn't display via HTML. Here is a sample encrypted image's attempt at being recovered: https://pastebin.com/miDCP3Gz

NOTE: My "long secret random key" includes a hashed random unique salt, but I am sure I am encrypting and decrypting with the same string.

Any idea why this wouldn't be displaying correctly?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试上传图像,加密图像,将其存储在MySQL中,然后解密显示 当授权人要求查看时。

以下是我目前正在加密的方式:

  $ image = addslashes(file_get_contents($  _FILES ['users_image'] ['tmp_name'])); 
 $ enc_image = encrypt($ image,“long secret random key”); 
   
 
 

然后 我将 $ enc_image 存储在MySQL BLOB字段中。 当我尝试解密并打印时,它就像这样:

  $ img = decrypt($ rec ['file'],“long secret random key”); 
echo  '&lt; img src =“data:image / jpeg; base64,'。base64_encode($ img)。'”/&gt;'; 
   
 
 

我是 使用此Stackoverflow答案中的代码,我在输出中看到解密的base-64文本,但它 不通过HTML显示。 以下是加密图像的示例尝试恢复: https://pastebin.com/miDCP3Gz

注意:我的“长秘密随机密钥”包含一个哈希随机唯一盐,但我确信我使用相同的字符串加密和解密。

任何想法 为什么这不能正确显示?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题