Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何加密base64编码值_Php_Encryption_Base64 - Fatal编程技术网

Php 如何加密base64编码值

Php 如何加密base64编码值,php,encryption,base64,Php,Encryption,Base64,在这里我使用文件上传,在这里我做了base64编码图像到 $encodeimage = base64_encode(file_get_contents($filename)); //这里我们得到了编码图像值**现在我得到了答案,之后我想加密base64编码值,我正在写下面的代码,但我不能得到加密值 <?php require_once 'Security.php'; define ("MAX_SIZE","1000"); $errors=0; $image =$_FIL

在这里我使用文件上传,在这里我做了
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*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();
        }
    }

    //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;
    }
}

如果问题是您或其他人无法解密,原因可能是您在注释集中使用AES 256,但在代码中您将算法设置为AES 128,您是指CRYPT_RIJNDAEL_256而不是CRYPT_RIJNDAEL_128吗


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

为什么要对整个图像进行JSON编码,然后对其加密流进行JSON编码?@HankyPanky,你说得对,我更正了它,混淆了标题。是的,我要对编码图像值进行加密?第一步:为什么?你的安全模型是什么?使用加密时,您试图防御的攻击场景是什么?“但我无法获取加密值”不足以继续。你能清楚地描述你得到了什么和你期望得到什么吗?您是否启用了错误报告?你试过调试它吗?$iv=mcrypt_create_iv($ivSize,mcrypt_DEV_uradom);#从一个随机源创建一个初始化向量(IV),这一行我得到空值加密函数你使用的是哪个OS和php版本?如果你添加var_dump($IV),你会得到什么;在创建$iv之后?你有错误吗?我确实复制了你的代码,在UbuntuUhow上使用PHP5.6测试代码时得到了一个IV和加密的数据如何在encript函数中使用var_dump(),以及如何返回上一页创建一个测试函数来只测试这一部分,例如通过在单元测试中运行这里的代码(或者在可以从命令行调用的函数中)