Possible Duplicate:
PHP 2-way encryption: I need to store passwords that can be retrieved
I plan to store foreign account information for my users on my website, aka rapidshare username and passwords, etc... I want to keep information secure, but I know that if I hash their information, I can't retrieve it for later use.
Base64 is decrypt-able so there's no point using that just plain off. My idea is to scramble the user and pass before and after it gets base64ed that way even after you decrypt it, you get some funny looking text if you try to decrypt. Is there a php function that accepts values that will make an unique scramble of a string and de-scramble it later when the value is reinputed?
Any suggestions?
working example
You should not encrypt passwords, instead you should hash them using an algorithm like bcrypt. This answer explains how to properly implement password hashing in PHP. Still, here is how you would encrypt/decrypt:
To Encrypt:
To Decrypt:
Warning: The above example encrypts information, but it does not authenticate the ciphertext to prevent tampering. You should not rely on unauthenticated encryption for security, especially since the code as provided is vulnerable to padding oracle attacks.
See also:
Also, don't just use a "password" for an encryption key. Encryption keys are random strings.
Demo at 3v4l.org:
The best idea to encrypt/decrypt your data in the database even if you have access to the code is to use 2 different passes a private password (
user-pass
) for each user and a private code for all users (system-pass
).Scenario
user-pass
is stored with md5 in the database and is being used to validate each user to login to the system. This user-pass is different for each user.system-pass
for the encryption/decryption of the data. This system-pass is the same for each user.To handle a string / array I use these two functions:
It's flexible as in you can store/send via URL a string or array because the string/array is serialzed before encryption.
Check out mycrypt(): http://us.php.net/manual/en/book.mcrypt.php
And if you're using postgres there's pgcrypto for database level encryption. (makes it easier to search and sort)
One thing you should be very aware of when dealing with encryption:
Trying to be clever and inventing your own thing usually will leave you with something insecure.
You'd probably be best off using one of the cryptography extensions that come with PHP.