2

1)给定一个密文矩阵,我想计算每一列的重合指数。我想通过堆叠将密文与自身进行比较,就像在wiki的这个例子中所做的那样:http ://en.wikipedia.org/wiki/Index_of_coincidence#Example

我已经有了用于通用 delta IC 的 PHP,我在想我可能需要做的就是改变一些事情来让它工作,有人可以帮我解决这个问题吗?

2)我已经有单独的 PHP 脚本,用于将文本操作成不同的块,但没有像矩阵一样堆叠它们。我如何获得脚本(在上面的第 1 部分)来测试所有可能的矩阵(假设矩阵很小)。

这是我到目前为止所得到的:

<?php
  $showTable = false;
  if (isset($_POST['submitx'])) {
    $showTable = true;
    $ciphertext = strtoupper($_POST['ciphertext']);
    // compress out spaces
    $i = 0;
    $cols = 10; 
    $cells = 1; 
    $compressed = '';
    while ( $ciphertext[ $i ])
    {
        if (( $ciphertext[ $i ] >= 'A' ) && ( $ciphertext[ $i ] <= 'Z' ) ) {
            $compressed .= $ciphertext[ $i ];
        }
        $i++;
    }
    $inLen = strlen( $compressed );

    // text is now collected without spaces in $compressed
    $history=array_fill(0,99,0);
    for ($shift = 1; $shift < 100; $shift++ ) {

        // test coincidence at this shift
        $coinc = 0;
        for ( $i = 0; $compressed[ $i + $shift ]; $i++ ) {
            if ( $compressed[ $i ] == $compressed[ $i + $shift ] ) $coinc++;
        }
        $coef = 100.0 * $coinc / ( 3.85 * ($inLen - $shift));
        $history[ $shift ] = $coef;
    }
      for ( $keyLen = 1; $keyLen < 50; $keyLen++ ) {
        $freqSum = 0.0;
        $index = $keyLen;
        $count = 0;
        while ( $index < 100 ) {
            // step through the history array by stepsize = keyLen
            $freqSum += $history[ $index ];
            $count++;
            $index += $keyLen;
        }
        $freqSum = $freqSum / $count; // average of all the values
        $history[ $index ] = $freqSum; // put it back for later
    }
    }
?>
4

0 回答 0