Hello so obviously this is not a question with code, I am very familiar with salt and md5 encryption in php. But I need a safe way to store sensitive information in mysql. using md5 its very simple but again not the safest way. any suggestion or examples please ? it needs to be stored in db, and of course have the possibility to be retrieved and read. the simplest way is what im looking for
-
Which data you call sensitive, for example? – Your Common Sense Apr 09 '13 at 16:17
-
`possibility to be retrieved and read` - just curious, did you try it with familiar md5? – Your Common Sense Apr 09 '13 at 16:18
-
to be pedantic, MD5 is not encryption, but hashing. This is different, because an MD5 hash is not expected to be "decrypted". – SirDarius Apr 09 '13 at 16:19
-
@SirDarius thanks for correcting me that was a stupid statement I made – cppit Apr 09 '13 at 16:27
-
2Quite simply: don't bother encrypting it. Once someone has gotten inside your database it is almost a certainty that they can see your source as well. The only way in which storing an encrypted version of something actually protects it is if the application uses asymmetric encryption, [public/private keys] and the private key is held somewhere else, preferably under lock and key. – Sammitch Apr 09 '13 at 16:41
2 Answers
public static function encrypt($string, $salt = NULL){
$mcrypt_iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$mcrypt_iv = mcrypt_create_iv($mcrypt_iv_size, MCRYPT_RAND);
$mcrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $salt, $string, MCRYPT_MODE_ECB, $mcrypt_iv);
$encoded = base64_encode($mcrypted);
return $encoded;
}
public static function decrypt($hash, $salt = NULL){
$mcrypt_iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$mcrypt_iv = mcrypt_create_iv($mcrypt_iv_size, MCRYPT_RAND);
$basedecoded = base64_decode($hash);
$mcrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $salt, $basedecoded, MCRYPT_MODE_ECB, $mcrypt_iv);
return $mcrypted;
}
The following code is an working example, and uses AES-128 Mode ECB. If you aren't any friendly with the encryption terms used, it isn't any major deal either. Just use the code. :)

- 1,880
- 1
- 18
- 15
If you are storing something in the DB encrypted, then you don't want to decrypt it. If you want to read it back, why bother encrypting it in the first place?
For encryption, the best way to ensure forward compatibility with future code and also to make it more secure is to use crypt().
There are various options you can pass to crypt(), if you're storing passwords I would probably use sha512 with a sufficiently high enough number for "rounds". Check out: http://php.net/crypt#example-4701 for examples.
Essentially, you get the hashed value from the DB and you can use it as your salt for plain text to verify whether the plain text is the same password as what's stored in the DB - it's one way encryption, no decryption (yet, anyway ;)

- 79
- 6
-
whats the point to encrypt stored data like account numbers for example if I cant retrieve it back?I understand for passwords its different. – cppit Apr 09 '13 at 16:47
-
Why do you want to encrypt account numbers then? The nature of encryption is to hide data, if you make it easy to decrypt it then you might as well have plain text :) – euxneks Apr 09 '13 at 18:36
-
1If the web server and the database are on different machines then there's some logic to this. Though even if only one server is compromised then you're probably pretty hosed. – Hans Jul 01 '13 at 20:20
-
If the web server and database are on a network that is not secure that is another issue entirely - in which case you'd want to have something like end to end encrypted communication. As described above, the question seemed to want to store the data encrypted in the DB and decrypt it before using it. It's kind of a weird request.. :\ – euxneks Jul 02 '13 at 23:43