我在这里使用文件上传,在这里我做了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;
}
}
答案 0 :(得分:0)
如果问题是您或其他人无法解密,原因可能是您在评论集中使用的是AES 256,但在代码中您将算法设置为AES 128,您的意思是CRYPT_RIJNDAEL_256而不是CRYPT_RIJNDAEL_128 ?
当您不以静态方式使用加密功能时,还有一个原因是您的加密功能是静态的吗?