Advertisement
cahyadsn

Caesar Cipher Example (with txt file process)

Nov 17th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2. /********************************
  3. -- Caesar Chiper Example
  4. -- CREATED BY : CAHYA DSN
  5. -- CREATED ON : 2015-11-17
  6. *********************************/
  7. ?>
  8. <!doctype html>
  9. <html>
  10.   <head>
  11.     <title>Caesar Chiper</title>
  12.   </head>
  13.   <body>
  14. <fieldset>
  15.   <legend>Source Data</legend>
  16.   <form enctype="multipart/form-data" method="POST">
  17.   Rotate<input type="number" name='rot' min="1" max="26" />[1-26]<br />
  18.   Send this file: <input name="userfile" type="file" />
  19.   <input type="submit" value="Send File" />
  20. </form>
  21. </fieldset>
  22. <?php
  23. function caesar($str, $n) {
  24.     $ret = "";
  25.     for($i = 0, $l = strlen($str); $i < $l; ++$i) {
  26.         $c = ord($str[$i]);
  27.         if (97 <= $c && $c < 123) {
  28.             $ret.= chr(($c + $n + 7) % 26 + 97);
  29.         } else if(65 <= $c && $c < 91) {
  30.             $ret.= chr(($c + $n + 13) % 26 + 65);
  31.         } else {
  32.             $ret.= $str[$i];
  33.         }
  34.     }
  35.     return $ret;
  36. }
  37.  
  38. function crack_caesar($str) {
  39.     $max = 0;
  40.     $weight = array(
  41.         6.51, 1.89, 3.06, 5.08, 17.4,
  42.         1.66, 3.01, 4.76, 7.55, 0.27,
  43.         1.21, 3.44, 2.53, 9.78, 2.51,
  44.         0.29, 0.02, 7.00, 7.27, 6.15,
  45.         4.35, 0.67, 1.89, 0.03, 0.04, 1.13);
  46.     $c = $s = array(
  47.         0, 0, 0, 0, 0,
  48.         0, 0, 0, 0, 0,
  49.         0, 0, 0, 0, 0,
  50.         0, 0, 0, 0, 0,
  51.         0, 0, 0, 0, 0, 0);
  52.     for($i = 0, $l = strlen($str); $i < $l; ++$i) {
  53.         $x = (ord($str[$i]) | 32) - 97;
  54.         if (0 <= $x && $x < 26) {
  55.             ++$c[$x];
  56.         }
  57.     }
  58.     for ($off = 0; $off < 26; ++$off) {
  59.         for ($i = 0; $i < 26; ++$i) {
  60.             if ($max < ($s[$off]+= 0.01 * $c[$i] * $weight[($i + $off) % 26])) {
  61.                 $max = $s[$off];
  62.             }
  63.         }
  64.     }
  65.     return ((26 - array_search($max, $s)) % 26)+1;
  66. }
  67.  
  68. if(isset($_FILES['userfile']['name']) && isset($_POST['rot'])){
  69.   $uploadfile=basename($_FILES['userfile']['name']);
  70.   if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  71.     echo "File is valid, and was successfully uploaded.\n";
  72.   } else {
  73.       echo "Possible file upload attack!\n";
  74.   }
  75.   $handle=@fopen($uploadfile,'r');
  76.   $contents = fread($handle, filesize($uploadfile));
  77.   fclose($handle);
  78.   $n=$_POST['rot'];
  79.   $msg=caesar($contents,$n);
  80.   $save_path = 'result'.date('YmdHis').'.txt';
  81.   $fp = @fopen($save_path, 'a');
  82.   fputs($fp, "$msg\n");
  83.   fclose($fp);
  84. ?>
  85. <fieldset>
  86.   <legend>Source Text Data</legend>
  87.   <textarea><?php echo $contents;?></textarea>
  88. </fieldset>
  89. <fieldset>
  90.   <legend>Result Text Data</legend>
  91.   <textarea><?php echo $msg; ?></textarea><br />
  92.   Rotate by
  93.   <input type="text" value="<?php echo crack_caesar($msg);?>" />
  94. </fieldset>
  95. <?php
  96. }
  97. ?>
  98. </body>
  99. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement