views:

5279

answers:

5

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 md5 their information, i can't retrieve it later to use.

Base64 is decrypt-able so theres 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 bullshit 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?

(btw, i've already pretty much secured myself from sql injects by disallowing many symbols in the URI and cleaning out any retreived variables)

+9  A: 
$key = 'password to (en/de)crypt';
$string = 'string to be encrypted';

To Encrypt:

base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));

To Decrypt:

rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0");

However, you should always hash (MD5, SHA1) passwords, preferably with some salt.

Alix Axel
by salt you mean adding some random bull inside it right?
jiexi
Yes, like sha1('salt' . $password . 'pepper');
Alix Axel
ok thanks =D appreciate the help
jiexi
@jiexi: No problem! ;)
Alix Axel
A: 

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.

Sebastian P.
A: 

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)

Josh Rice
A: 

This will only give you marginal protection. If the attacker can run arbitrary code in your application they can get at the passwords in exactly the same way your application can. You could still get some protection from some SQL injection attacks and misplaced db backups if you store a secret key in a file and use that to encrypt on the way to the db and decrypt on the way out. But you should use bindparams to completely avoid the issue of SQL injection.

If decide to encrypt, you should use some high level crypto library for this, or you will get it wrong. You'll have to get the key-setup, message padding and integrity checks correct, or all your encryption effort is of little use. GPGME is a good choice for one example. Mcrypt is too low level and you will probably get it wrong.

Ants Aasma
A: 

To handle a string / array I use these two functions:

function encryptStringArray ($stringArray, $key = "Your secret salt thingie") { $s = strtr(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), serialize($stringArray), MCRYPT_MODE_CBC, md5(md5($key)))), '+/=', '-_,'); return $s; }

function decryptStringArray ($stringArray, $key = "Your secret salt thingie") { $s = unserialize(rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode(strtr($stringArray, '-_,', '+/=')), MCRYPT_MODE_CBC, md5(md5($key))), "\0")); return $s; }

It's flexible as in you can store/send via URL a string or array because the string/array is serialzed before encryption.

Martin