2016-06-21 5 views
-2

를 암호화하는 방법, 여기 그 후 내가 원하는, base64 인코딩 이미지를했다 base64 인코딩 된 값을 암호화하려면 코드 아래에 글을 쓰고 있지만 암호화 된 값을 얻을 수 없습니까?내가 파일 업로드 사용하고 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_128 대신 CRYPT_RIJNDAEL_256을 의미합니까?

정적 함수로 암호화 기능을 사용하지 않는 이유는 무엇입니까?

+0

$ iv = mcrypt_create_iv ($ ivSize, MCRYPT_DEV_URANDOM); # 무작위 소스에서 초기화 벡터 (IV)를 만듭니다.이 라인에서는 널 값 암호화 기능을 얻고 있습니다 –

+0

어떤 OS와 PHP 버전을 사용하고 있습니까? – rypskar

+0

우분투를 사용하고 있습니다 –

관련 문제