Secure password encryption / string decryption in PHP

I wanted to know if there is a somewhat simple but secure method for encrypting strings (not passwords) with a password that is not stored on the server in PHP.

I checked the reversible password encryption procedure for PHP , but I'm not sure how secure it is if the attackers have access to the server and source.

We are talking about an automatic system in which a computer sends a request to a server that stores information in a log. Therefore, I think that I can send the encryption password to the request header, preferably encrypted, but then it would be difficult to decrypt it without saving the password somehow on the server. Wait, I think that I could complicate the situation too much, but I hope you understand this idea ... This means that the information will be safe, even if the hackers have full control over the server.

+8
string security php passwords encryption
source share
3 answers

If I understand you correctly, you will aim at the log encrypted by the server. Requests are sent in the normal mode, but you want to register something like access statistics for each user, etc., and you think that this data is confidential, therefore it should be encrypted by the server, and also be decrypted by the server, if necessary.

If so, it is actually not too complicated.

  • Generate an encryption key (AES will be a good choice) that will be used by the server.
  • You save this key in a file.
  • Make sure that the application and only a few select people have access to this location. In the worst case, it will be used in your public files, and anyone can download it from the Internet. So put it in a folder remote from your public resources :)
  • Encrypt this file using password-based encryption for example. PBKDF2 in RFC 2898 .

Then you will understand that you have created a problem with the chicken egg - the file again needs a password to access the key of the key stored inside. But here's the trick - you have to enter the key when starting the server manually and that you need an ephemeral component. The password for the file must be out-of-band information (for example, placed in physical storage) and nowhere else on the computer itself.

An alternative (but potentially less secure, since the password will be present in some physical form) is to rely on special "password stores" for the OS, such as Windows Isolated Storage .

+4
source share

One option for this that seems to fit your requirements is to use public / private key cryptography. If you had a user who encrypted the string using the public key, then the encrypted data stored on the server could not be able to decrypt the data to the attacker.

when / if you need to decrypt the data, just copy it to the place where you have the private key and use it to decrypt.

+2
source share

I would go with Mcrypt to encrypt / decrypt data in php.
My selection algorithm would be two. You will need a key to encrypt / decrypt data, and sending it using a request can be a security problem if you do not have ssl. If encryption should be performed on request, and not in real time, than just run the console script so that the password is not stored on the server.

The code for encryption / decryption is simple:

$encrypted= mcrypt_ecb(MCRYPT_TWOFISH, $key, $input, MCRYPT_ENCRYPT); $decrypted= mcrypt_decrypt(MCRYPT_TWOFISH , $key, $data, MCRYPT_DECRYPT); 
0
source share

All Articles