Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /********************************
- -- Caesar Chiper Example
- -- CREATED BY : CAHYA DSN
- -- CREATED ON : 2015-11-17
- *********************************/
- ?>
- <!doctype html>
- <html>
- <head>
- <title>Caesar Chiper</title>
- </head>
- <body>
- <fieldset>
- <legend>Source Data</legend>
- <form enctype="multipart/form-data" method="POST">
- Rotate<input type="number" name='rot' min="1" max="26" />[1-26]<br />
- Send this file: <input name="userfile" type="file" />
- <input type="submit" value="Send File" />
- </form>
- </fieldset>
- <?php
- function caesar($str, $n) {
- $ret = "";
- for($i = 0, $l = strlen($str); $i < $l; ++$i) {
- $c = ord($str[$i]);
- if (97 <= $c && $c < 123) {
- $ret.= chr(($c + $n + 7) % 26 + 97);
- } else if(65 <= $c && $c < 91) {
- $ret.= chr(($c + $n + 13) % 26 + 65);
- } else {
- $ret.= $str[$i];
- }
- }
- return $ret;
- }
- function crack_caesar($str) {
- $max = 0;
- $weight = array(
- 6.51, 1.89, 3.06, 5.08, 17.4,
- 1.66, 3.01, 4.76, 7.55, 0.27,
- 1.21, 3.44, 2.53, 9.78, 2.51,
- 0.29, 0.02, 7.00, 7.27, 6.15,
- 4.35, 0.67, 1.89, 0.03, 0.04, 1.13);
- $c = $s = array(
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0);
- for($i = 0, $l = strlen($str); $i < $l; ++$i) {
- $x = (ord($str[$i]) | 32) - 97;
- if (0 <= $x && $x < 26) {
- ++$c[$x];
- }
- }
- for ($off = 0; $off < 26; ++$off) {
- for ($i = 0; $i < 26; ++$i) {
- if ($max < ($s[$off]+= 0.01 * $c[$i] * $weight[($i + $off) % 26])) {
- $max = $s[$off];
- }
- }
- }
- return ((26 - array_search($max, $s)) % 26)+1;
- }
- if(isset($_FILES['userfile']['name']) && isset($_POST['rot'])){
- $uploadfile=basename($_FILES['userfile']['name']);
- if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
- echo "File is valid, and was successfully uploaded.\n";
- } else {
- echo "Possible file upload attack!\n";
- }
- $handle=@fopen($uploadfile,'r');
- $contents = fread($handle, filesize($uploadfile));
- fclose($handle);
- $n=$_POST['rot'];
- $msg=caesar($contents,$n);
- $save_path = 'result'.date('YmdHis').'.txt';
- $fp = @fopen($save_path, 'a');
- fputs($fp, "$msg\n");
- fclose($fp);
- ?>
- <fieldset>
- <legend>Source Text Data</legend>
- <textarea><?php echo $contents;?></textarea>
- </fieldset>
- <fieldset>
- <legend>Result Text Data</legend>
- <textarea><?php echo $msg; ?></textarea><br />
- Rotate by
- <input type="text" value="<?php echo crack_caesar($msg);?>" />
- </fieldset>
- <?php
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement