Reputation: 4197
so I have this website that allows users to write every day. It then get stocked in a database in plain text. It's not a blog so everything is private, and the biggest complain I regularly get is that "I" could still read what they wrote. It was still not "perfectly" private. Also I don't want to be the one who leaked thousand of private diaries.
So here is my train of thought on how to rend it private only to them.
When they log in : key = sha1(salt + password) and store this key in a SESSION (how secure is that ?)
When they save their text : encrypt it with their $_SESSION['key'] before saving it to the database
When they read something they've saved, decrypt it with their $_SESSION['key'] before displaying it.
Is that secure ? Also what is the best way to encrypt/decrypt UTF-8 ?
Also if someone changes its password it has to decrypt/re-crypt everything.
Upvotes: 3
Views: 3792
Reputation: 694
Do not use password to encrypt the key, password should never be used anywhere in the logic, and should only be read on login as a hash not plain text. You can user other things like user email to generate a key.
Upvotes: 0
Reputation: 10754
You should instead store the hash of the password in the SESSION.
Never store plain passwords anywhere - anywhere!!
Also, consider reading this stackoverflow thread: Secure hash and salt for PHP passwords
To hash the password, you can use this approach:
function generate_hash($password) {
$salt = "<some random string of characters>"; // do not change it later.
return md5($salt . $password);
}
For the encryption, you can use the mCrypt library. A typical algorithm can be:
$key = 'password to (en/de)crypt';
$string = 'string to be encrypted';
$encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
$decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
var_dump($encrypted);
var_dump($decrypted);
Upvotes: 3
Reputation: 2883
You should be using some form of encryption. PHP provides mCrypt for this purpose. Point by Point:
mcrypt
for this.I hope this helps!
Upvotes: 1