Answer the question
In order to leave comments, you need to log in
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
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.
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 :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question