1)给定一个密文矩阵,我想计算每列的重合指数。我想通过堆叠将密文与自身进行比较,如本例中wiki所做的那样:http://en.wikipedia.org/wiki/Index_of_coincidence#Example
我已经拥有了一般delta I.C的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
}
}
?>