M
M
Mellowtoy2016-04-19 18:43:35
linux
Mellowtoy, 2016-04-19 18:43:35

How to encrypt file on server using PHP?

Good evening!
I needed to write a simple file hosting service with the ability to encrypt all received files on the server and, accordingly, decrypt them when uploading, but, oddly enough, I did not find any manuals on the Internet that were more or less understandable to me on how to do this.
Therefore, I turn to you with a question. How to do it? Are there ready-made functions in PHP?
I heard about mcrypt, but it encrypts string data, and I need to encrypt files, and mp3s, and pictures, and whatever. How to do it?
Thank you.
UPD: Thanks for the answers, I got the point. I encrypt files through the means of linux itself, but then another question appeared. Where to store encryption keys? After the user uploads the file, I will have to encrypt it with the generated key, but I need to store it somewhere so that I can later decrypt it for return. Where to store it? It seems to me that storing it in the database on the same server is a completely unsafe option, and then the whole essence of encryption is lost. Correct me if I'm wrong.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivan Bogachev, 2016-04-19
@Mellowtoy

Alternatively, you can use not PHP functions, but something from Linux itself, for example gpg , calling it through shell_exec . This is probably not the best solution in terms of performance, but for a simple project it might be fine.

D
D', 2016-04-19
@Denormalization

You can use php.net/manual/ru/function.openssl-encrypt.php
Encrypt with some aes-256-cbc. Key + vector to generate for each file, and give them to the user so that only he can pick up the file :)

Z
zooks, 2016-04-19
@zooks

And what about PHP? You just call a Linux command with them via exec.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question