mercredi 22 juin 2016

How to encode the value in base64

Here I am using file upload,here I did base64 encode image upto

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

//here we got encoded image value** now I got answer,after that I want to encrypt the base64 encoded value,I am writing below code but I can't get encrypt value?

<?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*I8tNmd@x2oODwwDRr4&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();
        }
    }
 
    // Decrypt a value using AES-256.
    public static function decrypt($cipher, $key, $hmacSalt = null) {
        self::_checkKey($key, 'decrypt()');
        if (empty($cipher)) {
            echo 'The data to decrypt cannot be empty.'; die();
        }
        if ($hmacSalt === null) {
            $hmacSalt = self::$salt;
        }
 
        $key = substr(hash('sha256', $key . $hmacSalt), 0, 32); # Generate the encryption and hmac key.
 
        // Split out hmac for comparison
        $macSize = 64;
        $hmac = substr($cipher, 0, $macSize);
        $cipher = substr($cipher, $macSize);
 
        $compareHmac = hash_hmac('sha256', $cipher, $key);
        if ($hmac !== $compareHmac) {
            return false;
        }
 
        $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 = substr($cipher, 0, $ivSize);
        $cipher = substr($cipher, $ivSize);
        $plain = mcrypt_decrypt($algorithm, $key, $cipher, $mode, $iv);
        return rtrim($plain, "�");
    }
     
    //Get Random String - Usefull for public key
    public function genRandString($length = 0) {
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        $str = '';
        $count = strlen($charset);
        while ($length-- > 0) {
            $str .= $charset[mt_rand(0, $count-1)];
        }
		return $str;
    }
}

Aucun commentaire:

Enregistrer un commentaire