I was trying to to encrypt and decrypt a text file. I managed to get the encryption php page to work, which created a file I called encrypt.txt. However, I'm not so sucessful in decrypting the text file. On my decrypt php page I'm trying to have a form field where the encryption password is entered and once submitted the encrypt.txt file is decrypted and then displayed. I believe I have the decryption code some where in the ball park but could use some help. Here is what I have so far.

<body>

<?php
$messagedata = file_get_contents('encrypt.txt');
$cipher = mcrypt_module_open('blowfish', '', 'ecb', '');
$iv_size = mcrypt_enc_get_iv_size($cipher);
$iv = substr($messagedata, 0, $iv_size);
$ciphertext = substr($messagedata, $iv_size);
$password = "horse";
mcrypt_generic_init($cipher, $password, $iv);
$plaintext = mdecrypt_generic($cipher, $ciphertext);
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
echo
?>

<form method="get">
Password: <input type="password" name="pass" value="password"/><br /><br />
<input type="submit" name="submit" />
</form>

</body>

    I see some code and a form but I see no reference to any $_GET variables in the code. I also don't see any action attribute in your form.

    It might help to start with if you try to decrypt the file without retrieving the password from a form just to make sure it will in fact decrypt properly. Once you are sure you can decrypt it, then work on getting the password from a form.

      Correct there isn't a $_GET variable in my code. The code I have is from a book I have but in that section it doesn't explain how to do what I'm looking for. All the php coding in that part of the chapter general contains a plaintext line that is encrypt and then decrypt using another section of code. The plaintext get encrypted into a text file and the code I used is supposed to decrypted that text file but no where in that section of the book does it go into anything about using a form field to decrypt a file and display it. The decrypt code I use is almost word for word.

      My encryption worked because it was almost exactly as an example in the book I have with an exception of having to switch out a plaintext line for a $messagedata = file_get_contents. I know I gave to use a $_GET variable somewhere because I'm using a form field but not sure where to would go. Even with the code I currently have minus the form data, I can't tell if the decryption is happening when the doc is called because nothing is displayed. The book doesn't even show how the decryption page displays the decrypted data.

        Is that a question? Maybe you should read the book for a bit and come back when you have a more specific question? I feel it's a bit beyond me to explain PHP to you. If you ask me something more specific, I can answer.

          I finished the echo line to display the decrypted message and when the page is called it does display the decrypted text but with extra stuff &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533; on the end of the text line.

          <?php
          $messagedata = file_get_contents('encrypt.txt');
          $cipher = mcrypt_module_open('blowfish', '', 'ecb', '');
          $iv_size = mcrypt_enc_get_iv_size($cipher);
          $iv = substr($messagedata, 0, $iv_size);
          $ciphertext = substr($messagedata, $iv_size);
          $password = "horse";
          mcrypt_generic_init($cipher, $password, $iv);
          $plaintext = mdecrypt_generic($cipher, $ciphertext);
          mcrypt_generic_deinit($cipher);
          mcrypt_module_close($cipher);
          echo $plaintext
          ?>


          I also messed around with the form field and the above code to insert the submitted password. Now my code is:

          <?php
          $messagedata = file_get_contents('encrypt.txt');
          $cipher = mcrypt_module_open('blowfish', '', 'ecb', '');
          $iv_size = mcrypt_enc_get_iv_size($cipher);
          $iv = substr($messagedata, 0, $iv_size);
          $ciphertext = substr($messagedata, $iv_size);
          $password = $_POST[password];
          mcrypt_generic_init($cipher, $password, $iv);
          $plaintext = mdecrypt_generic($cipher, $ciphertext);
          mcrypt_generic_deinit($cipher);
          mcrypt_module_close($cipher);
          echo $plaintext
          ?><br /><br />

          <form action="decrypt.php" method="post">
          Password: <input type="password" name="password" value="password"/><br /><br />
          <input type="submit" name="submit" value="submit"/>
          </form>

          This gives me several errors:

          Warning: mcrypt_generic_init(): Key size is 0 in /home/....../assignment8/decrypt.php on line 24

          Warning: mcrypt_generic_init(): Key length incorrect in /home/....../assignment8/decrypt.php on line 24

          Warning: mdecrypt_generic(): 3 is not a valid MCrypt resource in /home/....../assignment8/decrypt.php on line 25

          Warning: mcrypt_generic_deinit(): 3 is not a valid MCrypt resource in /home/....../assignment8/decrypt.php on line 26

          Warning: mcrypt_module_close(): 3 is not a valid MCrypt resource in /home/....../assignment8/decrypt.php on line 27

          However, when I enter the password into the form and submit it, The errors go away and the decrypted text is properly displayed with extra stuff &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;.


          I'm also trying to called/ display the encrypted data prior to the execution of the decrypt text just to show that the encrypt.txt file has been encrypted. What I have doesn't work but here it is.

          if ($messagedata == "encrypt.txt") {
          echo "$messagedata";
          }

          Thanks

            Firstly, please use the code formatting tags supported by this forum (put [/code] after your code and

             before it) so that it's formatted as code.
            
            Next, I don't see 24 lines of code in either of those scripts you posted.  If you are having a problem with errors, read the documentation: [man]mcrypt_generic_init[/man], [man]mdecrypt_generic[/man], [man]mcrypt_generic_deinit[/man], [man]mcrypt_module_close[/man].
            
            When I read the docs, I can see that the 2nd parameter assigned to mcrypt_generic_init is a key.  If this key is the empty string (which it would be if you had not submitted your form) then you will get this error which probably leads to the other errors.  You need to alter your code so that the encryption/decryption are not executed unless you actually post the form.  You should also check the success or failure of various statements and halt code execution if they fail.  
            
            And finally, all this does is define a string variable that contains the text "encrypt.txt" and then display it:
            [code=php]
            if ($messagedata == "encrypt.txt") {
            echo "$messagedata";
            }
            

            If you are trying to find the contents of a file, use [man]file_get_contents[/man].

            Beyond that, I'm not really sure what the real issue is.

              Write a Reply...