jardel
jardel

Reputation: 35

Storing data in the db as crypted, and retrieving

For example:

$text = "sample";
$salt = "sh54mdR";

I'd like to store $text value in the db as encrypted by using $salt value. result should be like that:

jkhdnewewjhjjhnd (it's just an example, so I want to hide $text value)

than id like to de-crypt "jkhdnewewjhjjhnd" to "sample" I can't use md5, sha etc. I'm looking for a way to store the value and than de-crypt it. do you know a PHP function for my request?

Upvotes: 2

Views: 194

Answers (2)

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99879

Look at the mcrypt functions.

Encrypt:

$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";

$encrypted_data = mcrypt_ecb (MCRYPT_3DES, $key, $input, MCRYPT_ENCRYPT);

Decrypt:

$decrypted = mbcrypt_ecb(MCRYPT_3DES, $key, $encrypted_data, MCRYPT_DECRYPT);

See examples from http://us.php.net/manual/en/mcrypt.examples.php

Don't forget to use base64_encode() before storing in the database, unless the column accepts binary data.

Upvotes: 2

v42
v42

Reputation: 1425

use the Mcrypt library, it supports lots of ciphers (see the list here: http://www.php.net/manual/en/mcrypt.ciphers.php)

An example:

$text = "sample";
$salt = "sh54mdR";

$encrypted_data = mcrypt_ecb (MCRYPT_3DES, $salt, $text, MCRYPT_ENCRYPT);

Upvotes: 0

Related Questions