2016-06-21 101 views
-2

这里我使用的文件上传,在这里我也base64编码图像高达如何加密base64编码值

$encodeimage = base64_encode(file_get_contents($filename)); 

//这里我们得到了编码的图像值**现在我得到了答案,之后我想加密base64编码值,我写下面的代码,但我无法获得加密值?

<?php 
require_once 'Security.php'; 

define ("MAX_SIZE","1000"); 
$errors=0; 
    $image =$_FILES["file"]["name"];//i got filename here 
    $uploadedfile = $_FILES['file']['tmp_name']; 
    $filetype = $_FILES['file']['type']; 
     if ($image) 
     { 
     $filename = stripslashes($_FILES['file']['name']); 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
     { 
     $error_msg = ' Unknown Image extension '; 
     $errors=1; 
     } 
    else{ 
     $size=filesize($_FILES['file']['tmp_name']); 
     if ($size > MAX_SIZE*1024) 
     { 
     $error_msg = "You have exceeded the size limit"; 
     $errors=1; 
     } 

     if($extension=="jpg" || $extension=="jpeg") 
     { 
     $uploadedfile = $_FILES['file']['tmp_name']; 
     $src = imagecreatefromjpeg($uploadedfile); 
     } 
     else if($extension=="png") 
     { 
     $uploadedfile = $_FILES['file']['tmp_name']; 
     $src = imagecreatefrompng($uploadedfile); 
     } 
     else 
     { 
     $src = imagecreatefromgif($uploadedfile); 
     } 

     list($width,$height)=getimagesize($uploadedfile); 

     $newwidth=600; 
     /*$newheight=($height/$width)*$newwidth;*/ 
     $newheight=600; 
     $tmp=imagecreatetruecolor($newwidth,$newheight); 

     imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

     $filename = $_FILES['file']['name']; 

     imagejpeg($tmp,$filename,100); 

     $encodeimage = base64_encode(file_get_contents($filename));//here we got encodede image value 

     $encrypt_image = "data:".$filetype."base64,".$encodeimage; 

     $security = new Security(); 
     /*$string = $_POST['user_string'];*/ 
     $publicKey = $security->genRandString(32); 
     $encryptedData = $security->encrypt($encrypt_image, $publicKey); 

     imagedestroy($src); 
     imagedestroy($tmp); 
     } 
     } 

     function getExtension($str) { 

       $i = strrpos($str,"."); 
       if (!$i) { return ""; } 

       $l = strlen($str) - $i; 
       $ext = substr($str,$i+1,$l); 
       return $ext; 
     } 

     $id_proof = array("filename" =>$filename, 
          "base64_encodeimage" =>$encrypt_image, 
          "encryptedData" => $encryptedData,//getting null value here 
          "error_msg" =>$error_msg 
          ); 
     echo json_encode($id_proof); 
?> 

Security.php

<?php 
class Security { 

    // Private key 
    public static $salt = 'Lu70K$i3pu5xf7*[email protected]&xjuyTh'; 


    // Encrypt a value using AES-256. 
    public static function encrypt($plain, $key, $hmacSalt = null) { 
     self::_checkKey($key, 'encrypt()'); 

     if ($hmacSalt === null) { 
      $hmacSalt = self::$salt; 
     } 

     $key = substr(hash('sha256', $key . $hmacSalt), 0, 32); # Generate the encryption and hmac key 

     $algorithm = MCRYPT_RIJNDAEL_128; # encryption algorithm 
     $mode = MCRYPT_MODE_CBC; # encryption mode 

     $ivSize = mcrypt_get_iv_size($algorithm, $mode); # Returns the size of the IV belonging to a specific cipher/mode combination 
     $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM); # Creates an initialization vector (IV) from a random source 
     $ciphertext = $iv . mcrypt_encrypt($algorithm, $key, $plain, $mode, $iv); # Encrypts plaintext with given parameters 
     $hmac = hash_hmac('sha256', $ciphertext, $key); # Generate a keyed hash value using the HMAC method 
     return $hmac . $ciphertext; 
    } 

    // Check key 
    protected static function _checkKey($key, $method) { 
     if (strlen($key) < 32) { 
      echo "Invalid public key $key, key must be at least 256 bits (32 bytes) long."; die(); 
     } 
    } 

    //Get Random String - Usefull for public key 
    public function genRandString($length = 0) { 
     $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 
     $str = ''; 
     $count = strlen($charset); 
     while ($length-- > 0) { 
      $str .= $charset[mt_rand(0, $count-1)]; 
     } 
     return $str; 
    } 
} 
+1

为什么你想要JSON编码整个图像,然后加密的流呢? –

+0

@HankyPanky,你是对的,我纠正它,令人困惑的标题。 – Devon

+0

是的,我想加密编码的图像值? –

回答

0

如果问题是,你或者别人无法解密它的原因可能是你正在使用AES 256评语集,但在你的代码是将算法设置为AES 128,您的意思是CRYPT_RIJNDAEL_256而不是CRYPT_RIJNDAEL_128?

还有一个原因,当你不使用静态方式时,你的加密函数是静态的吗?

+0

$ iv = mcrypt_create_iv($ ivSize,MCRYPT_DEV_URANDOM); #从一个随机源创建一个初始化向量(IV),这一行我得到空值加密功能 –

+0

您使用哪种操作系统和PHP版本? – rypskar

+0

我使用的是Ubuntu –