06-07-2013, 09:30
|
|
|
|
חבר מתאריך: 15.04.06
הודעות: 18,034
|
|
בעיה עם הפונקציה mcrypt_decrypt ב php
כאשר הבעיה היא לא ההצפנה אלא להציג את הטקסט המקורי המוצפן באחד הקטעים.
הקוד כמובן לא ישאר ככה וזה קוד לניסוי בלבד.
הסבר מהיר על הקוד:
קטע 0, מקבל מידע מקובץ קיים ומעביר אותו מוצפן לקובץ החדש.
קטע 1, אמור להציג את התוכן המוצפן מהקובץ החדש בצורה לא מוצפנת.
קטע 2, מציג את התוכן המוצפן וזה עובד מעולה.
הפלט בקטע מספר 1 לא חוזר למקור בעוד שבקטע מספר אפס בשורה הזאת:
קוד:
echo $line_text . "<br />";
המקור יוצא בסדר גמור.
כל הקוד:
קוד PHP:
<?php echo 'start' . '<br />';
// open the unencrypt file // $file_pathb = 'basic.xml'; $unencrypt_file = fopen($file_pathb, "r+"); // open the encrypt file // $file_path = 'basicb.xml'; $encrypt_file = fopen($file_path, "w+"); echo crypt('lalalala', '$2a$04$thisisasaltthisisasaleVr5G5GFnohRYqG.J4XgL SnIgW49GUe.'); // $2a$04$thisisasaltthisisasaleVr5G5GFnohRYqG.J4XgLS nIgW49GUe. // $2a$04$thisisasaltthisisasale
# --- ENCRYPTION --- # the key should be random binary, use scrypt, bcrypt or PBKDF2 to # convert a string into a key # key is specified using hexadecimal $key = pack('H*' ,'$2a$04$thisisasaltthisisasaleVr5G5GFnohRYqG.J4Xg LSnIgW49GUe.'); # show key size use either 16, 24 or 32 byte keys for AES-128, 192 # and 256 respectively $key_size = strlen($key); echo '<br />' . "Key size: " . $key_size . "\n";
if(!$unencrypt_file) { echo "Could not open file: unencrypt_file $unencrypt_file in read mode\n"; exit(1); } if(!$encrypt_file) { echo "Could not open file: encrypt_file $encrypt_file in write mode\n"; exit(1); } else { // unencrypt file: // //echo '<br />' . 'encrypt file:' . '<br />' . '<br />'; while(!feof($unencrypt_file)) { $line_text = fgets($unencrypt_file); # create a random IV to use with CBC encoding $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); # creates a cipher text compatible with AES (Rijndael block size = 128) # to keep the text confidential # only suitable for encoded input that never ends with value 00h # (because of default zero padding) $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $line_text, MCRYPT_MODE_CBC, $iv); # prepend the IV for it to be available for decryption $ciphertext = $iv . $ciphertext; # encode the resulting cipher text so it can be represented by a string $ciphertext_base64 = base64_encode($ciphertext); // print the output: //echo $ciphertext_base64 . "\n"; fwrite($encrypt_file, $ciphertext_base64 ); # === WARNING === # Resulting cipher text has no integrity or authenticity added # and is not protected against padding oracle attacks. # --- DECRYPTION --- $ciphertext_dec = base64_decode($ciphertext_base64); # retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv_dec = substr($ciphertext_dec, 0, $iv_size); # retrieves the cipher text (everything except the $iv_size in the front) $ciphertext_dec = substr($ciphertext_dec, $iv_size); # may remove 00h valued characters from end of plain text $line_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); //echo $plaintext_utf8_dec . "\n"; //print a output: //echo $line_text . "<br />"; } // encrypt file: // fclose($unencrypt_file); fclose($encrypt_file); } //print($encrypt_file); echo '<br />' . $encrypt_file . '<br />'; // print file // echo '<br />' . 'start 2:' . '<br />' . '<br />' . '<br />'; // ------------------------------------------------------ // // 1 $encrypt_file = fopen($file_path, "r"); $line_text=''; if(!$encrypt_file) { echo "Could not open file: unencrypt_file $encrypt_file in write mode\n"; exit(1); } else { echo '<br />' . '<br />' . "unencrypt file:" . '<br />' .'<br />'; while(!feof($encrypt_file)) { //echo $plaintext_utf8_dec . "\n"; $line_text = fgets($encrypt_file); # --- DECRYPTION --- $ciphertext_dec = base64_decode($ciphertext_base64); # retrieves the IV, iv_size should be created using mcrypt_get_iv_size() $iv_dec = substr($ciphertext_dec, 0, $iv_size); # retrieves the cipher text (everything except the $iv_size in the front) $ciphertext_dec = substr($ciphertext_dec, $iv_size); # may remove 00h valued characters from end of plain text $line_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec); // שתי השורות הבאות מחזירות אותו דבר פלט קצר: //echo $line_text . '<br />'; echo $ciphertext_dec . '<br />'; } fclose($encrypt_file); } // ------------------------------------------------------ // // 2 $encrypt_file = fopen($file_path, "r"); $line_text=''; if(!$encrypt_file) { echo "Could not open file: encrypt_file $encrypt_file in write mode\n"; exit(1); } else { echo '<br />' . '<br />' . '<br />' . '<br />' . "encrypt file:" . '<br />' .'<br />' . '<br />' . '<br />'; while(!feof($encrypt_file)) { $line_text=fgets($encrypt_file); echo $line_text . '<br />'; } fclose($encrypt_file); } echo '<br />' . '<br />' . '<br />' . $encrypt_file . '<br />'; echo '<br />' . '<br />' . '<br />' . '<br />' . 'end'; ?>
מה הבעיה כאן?
יכול להיות שזה בגלל שאני יושב מול המסך וקורא קוד כבר 12 שעות וזה סתם דבר קטן או בעיה אחרת שלא ראיתי בקוד.
_____________________________________
will you marry me nesli barda? Operating system: Linux The day Microsoft & apple makes a product that does not suck will be the day they start making vacuum cleaners. say no to Apple&Facebook&MS&Samsung IL&Nokia&Hot&Yes,but say yes to Google
|