2-way encryption in PHP - you need to be guided

I am working on my first secure shopping site. We do not store credit card information, so this is not a problem. However, we have a transaction key and an API login key for our payment gateway (authorize.net), which I would prefer to store in the database rather than hard-code into my php. I don’t know that we need great security, but I would prefer not to store it in plain text. I know about sha, but unilaterally. I need a way to store a value in a database in a semi-secure format, but then be able to "decrypt" it programmatically for use in my function.

Another caveat is that my site is hosted, which means a very limited restriction on what I can install, so ideally any solution would rely on something that is part of the standard php installation.

Can someone point me in the right direction? I am very new to providing data.

EDIT ADD: I checked with my host and mcrypt is installed. Is this the right direction?

+6
php mysql hash
source share
3 answers

MCrypt can be your friend here. However, keep in mind that for every public (and useful) encryption method, a key is required. If AES encryption or 3DES encryption did not require a key during the encryption process, then breaking the encryption would be just an attempt at every standard decryption method until you get a meaningful result. Thus, storing a key for your payment gateway carries the same risks as storing a key for your encryption. Regardless of how many encryption layers you want to add, at some level there should be a key stored in plain text, usually hardcoded in PHP and often in the included config.php , in order to simplify its change into the future.

The only option to safely store information without the need for a key is to create your own encryption method. The security of this method lies exclusively in the fact that no one knows the means by which you encrypt the string, so they do not have a phased template to just go back. If you ever told someone how your encryption works, then security will be denied. In addition, there are many algorithmic ways to break simple encryption (for example, replacing letters). That's why mathematicians get a lot of money to develop things like AES.

It is best to look into MCrypt Encrypt and MCrypt Decrypt . That way, if only your PHP is compromised, then they know the key you used for encryption, but they have no data. If only the database is compromised, then they have data, but not the key that you used for encryption. If both are compromised, you are screwed. But if both of them are compromised, you are screwed up no matter what you do so that there is a fairly safe route.

+2
source share

Hmm, you can try AES encryption. The problem is that you need to save the salt hash (98sdfx9c6v5c) somewhere in your PHP.

Paste configuration:

 INSERT INTO config (secret_key) VALUES (AES_ENCRYPT('secret api key','98sdfx9c6v5c')); 

select configuration:

 SELECT AES_DECRYPT(secret_key,'98sdfx9c6v5c') AS secret_url FROM config 
0
source share

From a security point of view, there is no difference storing it in php files or in the database if someone has access to your php files, to which he has access to the database.

working with mcrypt does not mean that you will have MORE security (if they can read your php files, they can read the key too), so ...

If I were you, I would save the API key as plain text in a file outside the web server directory.

just write a good code, you should be fine.

-2
source share

All Articles