Cypher Finder PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 12:34 p.m.

FORMAT: Text only

SIZE: 2.9 kB

HITS: 804

  1. <?php
  2. /************************************************************************
  3. +----------------------------------------------------------------------+
  4. | ioc.php -> Index of Coincidence |
  5. +----------------------------------------------------------------------+ |
  6. | |
  7. | (c) 2002 by M.Abdullah Khaidar (khaidarmak@yahoo.com) |
  8. | |
  9. | This program is free software. You can redistribute it and/or modify |
  10. | it under the terms of the GNU General Public License as published by |
  11. | the Free Software Foundation; either version 2 of the License. |
  12. | |
  13. +----------------------------------------------------------------------+
  14. ************************************************************************/
  15. include "main.php";
  16. $TITLE="Index of Coincidence";
  17. $HEADTITLE="Index of Coincidence";
  18. top();
  19. echo "<form name=\"Form\" action=\"ioc.php\" method=\"post\">\n";
  20. echo "Enter text you want to calculate:<br>\n";
  21. echo "<textarea name=\"plain\" cols=40 rows=7 class=\"txtcolor\">\n";
  22. if ($plain){
  23. $plain=filter($plain);
  24. $plain=check_length($plain);
  25. echo $plain;
  26. }
  27. echo "</textarea><br><br>\n";
  28. echo "Key Length:";
  29. echo "<select name=\"keylength\" width=200 class=\"txtcolor\">\n";
  30. for($i=1;$i<=$MAXKEYLENGTH;$i++){
  31. if ($i==$keylength){
  32. echo "<option value=$i selected>$i</option>\n";
  33. }else{
  34. echo "<option value=$i>$i</option>\n";
  35. }
  36. }
  37. echo "</select>\n";
  38. echo "<br><br>";
  39. echo "<input type=\"submit\" name=\"process\" value=\"Calculate\" class=\"txtcolor\">&nbsp;&nbsp;\n";
  40. echo "</form>\n";
  41. if ($process=="Calculate"){
  42. $plain=filter($plain);
  43. $plain=check_length($plain);
  44. for($i=0;$i<26;$i++){
  45. for($j=1;$j<=$keylength;$j++){
  46. $freq[$i][$j]=0;
  47. }
  48. }
  49. for($i=0;$i<strlen($plain);$i=$i+$keylength){
  50. $pblock=substr($plain,$i,$keylength);
  51. for($m=1;$m<=$keylength;$m++){
  52. $p=substr($pblock,$m-1,1);
  53. $p=decimal($p);
  54. $freq[$p][$m]++;
  55. }
  56. }
  57. for($m=1;$m<=$keylength;$m++){
  58. $sum=0;
  59. $n=0;
  60. for($p=0;$p<26;$p++){
  61. $sum=$sum + $freq[$p][$m]*($freq[$p][$m]-1);
  62. $n=$n+$freq[$p][$m];
  63. }
  64. if($n==1||$n==0){
  65. $ioc[$m]="division by zero";
  66. }else{
  67. $ioc[$m]=$sum/($n*($n-1));
  68. }
  69. }
  70. echo "Index of coincidence with m=$keylength:<br>";
  71. for($m=1;$m<=count($ioc);$m++){
  72. echo number_format($ioc[$m],3)."<br>";
  73. }
  74. }
  75. bottom();
  76. ?>

comments powered by Disqus