emfpc's avatar
Level 1

Encryp Image to later read in view

Currently, using a form, I let the user upload a picture. This picture is later store in the Database. I'm trying to encrypt de data that goes to the Database plus the image. In my controller i doing this:

$user->identification = Crypt::encrypt($request->input('identification'));
if($img = $request->file('identification')){
                $img->storeAs('upload/images/Identification Files', "Ticket #".$ticket->ticket_number."_".$completname.".".$img->getClientOriginalExtension());
}

In the database it show the name of the identification field is encrypted but the image is not.

Try this one instead:

$user->identification = Crypt::encrypt($request->input('identification'));
$img = Crypt::encrypt($request->file('identification')->get());
Storage::put('upload/images/Identification Files/'.'Ticket#'.$ticket->ticket_number.'_'.'IdentificationOf_'.$completname,$img);

with this i was able to change the image but in order to read it on a view need to put the name of the file by hand in order to read it: *In User Controller @index:

$encryptedContent = Storage::get('upload/images/Identification Files/*NameOfTheFile');
$decryptedContent = decrypt($encryptedContent);
return view('photo', compact('encryptedContent', 'decryptedContent'));

*in the Photo view:

<img src="data:image/png;base64,{!! base64_encode($decryptedContent) !!}" alt="" />

The last one show the image but i'm trying to get the image to display when i looking for a particular user without inputing the name of the file is Storage::get.

Help will be appreciate. If is not easy to understand please let me know.

0 likes
11 replies
Snapey's avatar

Please go back to your original question and format your code by putting 3 backticks ``` on a line before and after each code block

Snapey's avatar

You need to store the file with a filename that you can reconstruct (or just store the filename)

This problem has nothing to do with encryption.

There are hundreds of image upload tutorials out there. Start with one of these, then add encryption when you are happy you know how uploading files works.

emfpc's avatar
Level 1

Already know how to upload the images. i was able to encrypt the images is the way to read it that can't find a way to put just the name of the file.

emfpc's avatar
Level 1

I'm able to upload images to the storage. Made a link to the public folder. the name is encrypted but i am able to see the picture when i use the Image tag

Snapey's avatar

Already know how to upload the images.

Not sure what you want help with then?

If you must encrypt the image file, then you must decrypt it and stream it to the client rather than letting the client get it direct. I don't see the point in encrypting the filename?

emfpc's avatar
Level 1

The images are sensitive since the have information of the users. I thought in order to protect the data I need to encrypted the file and decrypted when the authentic user is log in.

BayronVazquez's avatar

In the example shown below, all uploaded images are encrypted using a file type input field, the file name is a unique hash depending on the content of the image, this guarantees that there will be no duplicate files.

public function store(Request $request)
{
    $request->validate(
        [
            'name'          => 'required|max:100|min:4',
            'nationality'   => 'required|max:50',
            'pictures'      => 'required'
        ]
    );

    $images = $request->file('pictures');
    $allowedfileExtension = ['jpg','png','gif','jpeg','webp'];

    foreach($images as $image)
    {
        $extension    = $image->extension();
        $hash         = hash('md5', $image->get());
        $filename     = "$hash.$extension";
        $filesize     = $image->getSize() / 1024; // Filesize in MegaBytes
        $originalName = $image->getClientOriginalName();

        if( !in_array($extension,$allowedfileExtension) )
            throw ValidationException::withMessages(['pictures' => "The File: '$originalName' extension is invalid"]);

        $encryptedImageBytes = Crypt::encryptString($image->get());
        Storage::put("/public/directory/$filename", $encryptedImageBytes);
    }
    return redirect()->back();
}

then to get the content of the image do this:

public function show()
{
    $model = Model::where('user',1)->first();
    $base = base64_encode(
        Crypt::decryptString(
            Storage::get('public/directory/$model->filename')
        )
    );    
    return view('pages.person.show')->with('base',$base);
}

Now to finish in the view it would be something like this:

<img src="data:image/png;base64, {{$base}}" alt="" style="width: 200px">

Please or to participate in this conversation.