0

Hello Everyone Help needed, Has anyone implemented frequency analysis attack on caesar cipher in PHP and predicting the possible key for it. it should input the cipher text and predict the key using frequency analysis chart.

2
Contributors
2
Replies
22
Views
1 Year
Discussion Span
Last Post by cereal
0

this breaks but i want to do frequency analysis attack on it.

<?php

error_reporting(0);
$do=$_REQUEST["do"];
if($do=="break"){
    $alpha=array(
    array("0","A"),array("1","B"),array("2","C"),
    array("3","D"),array("4","E"),array("5","F"),
    array("6","G"),array("7","H"),array("8","I"),
    array("9","J"),array("10","K"),array("11","L"),
    array("12","M"),array("13","N"),array("14","O"),
    array("15","P"),array("16","Q"),array("17","R"),
    array("18","S"),array("19","T"),array("20","U"),
    array("21","V"),array("22","W"),array("23","X"),
    array("24","Y"),array("25","Z")
    );  // $alpha[20][1]= words    $alpha[20][0]=numbers
    $text=$_REQUEST["text"];
    $text_len=strlen($text);
    for($h=1; $h<26; $h++){
        for($i=0; $i<$text_len; $i++){
        $chr=$text[$i];
            for($j=0; $j<=24; $j++){ // search the arry for char
                if($alpha[$j][1]==$chr){
                        $res=($h+$alpha[$j][0])%25;
                        echo($alpha[$res][1]);
                }else if($chr==" "){
                        echo(" "); // replace spaces
                }
            }
        }echo("<br />");
                echo("===================================================================");
                echo("<br />");
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Caesar Cipher Manual Breaking</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body><center>
<form id="form1" name="form1" method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>?do=break">
  <p><strong>Caesar Cipher Manual Breaking</strong> <br />
    <br />
    Cipher
    <textarea name="text" id="text" cols="45" rows="5"></textarea>
  <br />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Break" />
  </p>
</form>
<p><a href="Caesar-Cipher-Encrypt.php">CaesarCipher Encrypt</a> &nbsp;|&nbsp; <a href="Caesar-Cipher-Decrypt.php">CaesarCipher Decrypt</a> &nbsp;| &nbsp;<a href="Caesar-Cipher-Breaking.php">CaesarCipher Manual Break</a></p>
<br />
</center>
</body>
</html>
0

Hi,

I don't understand what it should do your script, tested with a ciphered string and prints only ====. If you are searching for character frequency, then look at count_chars():

For example:

<?php

$message    = 'pda sknhz eo behhaz sepd oaynapo wjz iuopaneao fqop swepejc pk xa zeoykranaz';
$stats      = count_chars($message, 1);
$chars      = array_map('chr', array_keys($stats));
$frequency  = array_combine($chars, array_values($stats));

arsort($frequency);
print_r($frequency);

returns:

Array
(
    [ ] => 12
    [a] => 9
    [e] => 7
    [p] => 7
    [o] => 7
    [z] => 5
    [n] => 4
    [s] => 3
    [k] => 3
    [h] => 3
    [j] => 2
    [w] => 2
    [d] => 2
    [y] => 2
    [b] => 1
    [u] => 1
    [x] => 1
    [r] => 1
    [i] => 1
    [f] => 1
    [q] => 1
    [c] => 1
)

Example taken from: https://www.khanacademy.org/computing/computer-science/cryptography/ciphers/e/caesar_cipher_frequency_analysis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.