0

Possible Duplicate:
Two-way encryption in PHP

I need a PHP script that encrypts a variable with password. I don't mean hash like md5($var); or sha1($var);

I need a script that could make (for example) md5($var); hash but also get from md5($var); the useful string.

Expectation like

$password = "SomePassword"; 
$data = "TheVerySecretString";
$encrypted = TheEncyptionFunctionINeed($password, $data); // Output some useless strings
$decrypted = TheDecryptionFunctionINeed($password, $data); // Output: "TheVerySecretString"
Community
  • 1
  • 1
Rik Telner
  • 179
  • 1
  • 10

2 Answers2

3

Two-way encryption in PHP

Sry to open this up a couple years later, but I think it's important since it's in the top search rankings...

PHP 5.3 has introduced a new encryption method that is really easy to use.

It's openssl_encrypt and openssl_decrypt...It's not well documented here, so here's a simple example..

$textToEncrypt = "My super secret information.";
$encryptionMethod = "AES-256-CBC";  // AES is used by the U.S. gov't to encrypt top secret documents.
$secretHash = "25c6c7ff35b9979b151f2136cd13b0ff";

//To encrypt
$encryptedMessage = openssl_encrypt($textToEncrypt, $encryptionMethod, $secretHash);

//To Decrypt
$decryptedMessage = openssl_decrypt($encryptedMessage, $encryptionMethod, $secretHash);

//Result
echo "Encrypted: $encryptedMessage <br>Decrypted: $decryptedMessage";
Community
  • 1
  • 1
Danilo Kobold
  • 2,562
  • 1
  • 21
  • 31
0

Here are 2 functions:


function encryptData($value){
   $key = "top secret key";
   $text = $value;
   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
   return $crypttext;
}

function decryptData($value){
   $key = "top secret key";
   $crypttext = $value;
   $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
   $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
   $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
   return trim($decrypttext);
}
?>

check the manual for functions: mcrypt_encrypt and mcrypt_decrypt

Ivelin
  • 12,293
  • 5
  • 37
  • 35