Solved

Encrypting/decrypting passwords in plain-text files

Posted on 2007-03-18
4
951 Views
Last Modified: 2008-02-01
Hello all,

I have a settings file for my website which contains an array with important settings, and I want to have several of the values encrypted from plain view and then decrypted when I need to use them (e.g. the passwords to access databases etc.). I've never used the Mcrypt functions in PHP, so I've been trying a small test where I encrypt a variable (entered into a form) which is then encrpyted and written to a file on my server, and then decrypt the variable written to the file. My problem is that when I decrypt the variable, it is not the same as I originally entered.

--encryption--

$fp = fopen('./file.php', 'w');

fwrite($fp, "<?php\n\n\$variable = '" . mcrypt_encrypt(MCRYPT_RIJNDAEL_256, mhash(MHASH_MD5, 'tirg412e1dw'), $_POST['variable'], MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND)) . "';\n\n?>");

fclose($fp);

--decryption--

require('./file.php');

echo '<p>$variable = ' . mcrypt_decrypt(MCRYPT_RIJNDAEL_256, mhash(MHASH_MD5, 'tirg412e1dw'), $variable, MCRYPT_MODE_CBC, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC), MCRYPT_RAND)) . '.</p>';

--end--

I'm assuming that its a problem with the cipher, mode or IV, but I was also wondering if it was a problem that I'm writing it to a file and the character codes were somehow being lost.

Thanks for any help,  Adam.
0
Comment
Question by:madscientist
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Vel Eous
ID: 18744654
0
 
LVL 24

Accepted Solution

by:
slyong earned 125 total points
ID: 18744909
Try this:

//---- To Encrypt ---
<?php
$cipher = "rijndael-128";
$mode = "cbc";
$plain_text = "Hello World";
$secret_key = "01234567890abcde";
$iv = "fedcba9876543210";

$td = mcrypt_module_open($cipher, "", $mode, $iv);
mcrypt_generic_init($td, $secret_key, $iv);

$cyper_text = mcrypt_generic($td, $plain_text);
$hex_text = bin2hex($cyper_text);

$fp = fopen('./file.php', 'w');
fwrite($fp, "<?php\n\n\$variable='".$hex_text."';"."\n\n?>");
fclose($fp);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);
?>

//---- To Decrypt ---
<?php
function hex2bin($hexdata) {
  $bindata="";
 
  for ($i=0;$i<strlen($hexdata);$i+=2) {
   $bindata.=chr(hexdec(substr($hexdata,$i,2)));
  }

  return $bindata;
}

require('./file.php');

$cipher = "rijndael-128";
$mode = "cbc";
$secret_key = "01234567890abcde";
$iv = "fedcba9876543210";

$td = mcrypt_module_open($cipher, "", $mode, $iv);

mcrypt_generic_init($td, $secret_key, $iv);
$decrypted_text = mdecrypt_generic($td, hex2bin($variable));
echo '<p>$variable = ' . trim($decrypted_text). '</p>';
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
?>
0
 

Author Comment

by:madscientist
ID: 18744911
<sarcasm>I never thought of looking at the PHP manual...</sarcasm>
0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 18745149
Obviously not well enough then.

Have a nice day.  :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question