ernieboy Posted April 4, 2007 Share Posted April 4, 2007 First of all hello to all! It's my first time here and I'm also a php newbie. I have a problem with an encryption script which I'm sure someone will have a solution to. I'm using mcrypt to encrypt and decrypt an XML file. It all works fine except for one thing: during decryption the first line or two of the decrypted file has corrupt data. Have you ever seen this before and do you know what I might be doing wrong?. Please see http://www.ernieboy.com/order_example_recovered_xml.txt. Here is the code to enrcrypt <?php $str_error_msg; if($_SERVER['REQUEST_METHOD']== 'POST') { $raw_file = $_POST['raw_file']; $the_key = $_POST['the_key']; $cipher_file = $_POST['cipher_file']; if((strlen($the_key)) && (strlen($raw_file)) && (strlen($cipher_file)) ) { $source_file = $raw_file; $file_content = file_get_contents($source_file); /* Open the cipher */ $td = mcrypt_module_open('rijndael-256', '', 'cbc', ''); /* Create the IV and determine the keysize length, used MCRYPT_RAND * on Windows instead. Use MCRYPT_DEV_RANDOM in Linux */ $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); /* Create key */ $key = substr(md5($the_key), 0, $ks); /* Intialize encryption */ mcrypt_generic_init($td, $key, $iv); /* Encrypt data */ $encrypted = mcrypt_generic($td, $file_content); $cipher_destination = fopen($cipher_file , "w"); if(fwrite($cipher_destination, $encrypted)) { echo "Written to disk."; }//end if /* Terminate encryption handler */ mcrypt_generic_deinit($td); /* Initialize encryption module for decryption */ mcrypt_generic_init($td, $key, $iv); /* Decrypt encrypted string */ $decrypted = mdecrypt_generic($td, $encrypted); /* Terminate decryption handle and close module */ mcrypt_generic_deinit($td); mcrypt_module_close($td); /* Show string */ //echo nl2br( htmlspecialchars(trim($decrypted))) . "\n"; } else { $str_error_msg = "<p class=\"error\">Please fill all fields of the form in before you can encrypt anything.</p>"; }//end if }//end if ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Crypto test</title> <style type="text/css"> .error { color : red ;} </style> </head> <body> <h3>Welcome to crypto.</h3> <?=$str_error_msg?> <form method="post" action="<?php $_SERVER['PHP_SELF']?>"> <!-- MAX_FILE_SIZE must precede the file input field --> <label for="raw_file">Enter full path of file to encrypt e.g. c:\test.txt: <em> max 256 chars</em> </label><br /><input type="text" name="raw_file" id="raw_file" size="60" maxlength="255" /> <br /> <label for="the_key">Enter secret key: <em> max 256 chars</em></label><br /><input type="text" name="the_key" id="the_key" size="60" maxlength="255" /> <br /><label for="cipher_file"> Enter full path of encrypted file e.g. c:\test.enc: <em> max 256 chars</em></label><br /><input type="text" name="cipher_file" id="cipher_file" size="60" maxlength="255" /> <br /><br /><input type="submit" name="Encrypt" value="Encrypt"/> </form> <p>Go to the <a href="data_decrypter.php">decryption page.</a>.</p> </body> </html> Decryption code follows <?php $str_error_msg; if($_SERVER['REQUEST_METHOD']== 'POST') { $encrypted_file = $_POST['encrypted_file']; $the_key = $_POST['the_key']; $deciphered_file = $_POST['deciphered_file']; if((strlen($the_key)) && (strlen($encrypted_file)) && (strlen($deciphered_file)) ) { $source_file = $encrypted_file; $file_content = file_get_contents($source_file); /* Open the cipher */ $td = mcrypt_module_open('rijndael-256', '', 'cbc', ''); /* Create the IV and determine the keysize length, used MCRYPT_RAND * on Windows instead. Use MCRYPT_DEV_RANDOM in Linux */ $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); $ks = mcrypt_enc_get_key_size($td); /* Create key */ $key = substr(md5($the_key), 0, $ks); /* Initialize encryption module for decryption */ mcrypt_generic_init($td, $key, $iv); /* Decrypt encrypted string */ $decrypted = mdecrypt_generic($td, $file_content); $content_destination = fopen($deciphered_file , "w"); if(!fwrite($content_destination , $decrypted )) { echo "Error writing file."; }//end if /* Terminate decryption handle and close module */ mcrypt_generic_deinit($td); mcrypt_module_close($td); /* Show string */ // echo nl2br( htmlspecialchars(trim($decrypted))) . "\n"; } else { $str_error_msg = "<p class=\"error\">Please fill all fields of the form in before you can encrypt anything.</p>"; }//end if }//end if ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Decrypter test</title> <style type="text/css"> .error { color : red ;} </style> </head> <body> <h3>Welcome to decrypter.</h3> <?=$str_error_msg?> <form method="post" action="<?php $_SERVER['PHP_SELF']?>"> <label for="encrypted_file">Enter full path of the file to be decrypted e.g. c:\test.enc: </label><br /> <input type="text" name="encrypted_file" id="encrypted_file" size="60" maxlength="255" /><br /> <label for="the_key">Enter secret key: <em> max 256 chars</em></label><br /> <input type="text" name="the_key" id="the_key" size="60" maxlength="255" /><br /> <label for="deciphered_file">Enter full path of the newley-decrypted file: <em> max 256 chars</em></label><br /> <input type="text" name="deciphered_file" id="deciphered_file" size="60" maxlength="255" /> <br /><br /><input type="submit" name="Decrypt" value="Decrypt"/> </form> <p>Go to the <a href="data_encrypter.php">encryption page.</a>.</p> </body> </html> Any insights would be greatly appreciated. Thanks, Ernest Link to comment https://forums.phpfreaks.com/topic/45583-mcrypt-corrupts-first-line-of-decrypted-file-please-help/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.