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 ������� 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 �������.
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