Monoalphabetic Substitution PHP


SUBMITTED BY: Guest

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

FORMAT: Text only

SIZE: 4.7 kB

HITS: 840

  1. <?php
  2. /************************************************************************
  3. +----------------------------------------------------------------------+
  4. | mono.php -> Monoalphabetic Substitution |
  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="Monoalphabetic Substitution Encoder and Decoder";
  17. $HEADTITLE="Monoalphabetic Substitution Encoder and Decoder";
  18. function mono($plain,$key){
  19. $unique_table=1;
  20. $detected_table="";
  21. $from="abcdefghijklmnopqrstuvwxyz";
  22. $cipher="";
  23. if(eregi("--",$key)){
  24. return "error: Empty substitution detected";
  25. }
  26. for($i=0;$i<=25;$i++){
  27. $k=substr($key,$i,1);
  28. $check_table[$i]=$k;
  29. }
  30. for($i=0;$i<=25;$i++){
  31. for($j=$i+1;$j<=25;$j++){
  32. if($check_table[$i]==$check_table[$j]){
  33. $unique_table=0;
  34. $detected_table=$detected_table.ascii_letter($i).ascii_letter($j);
  35. }
  36. }
  37. }
  38. $plain=filter($plain);
  39. $cipher=strtr($plain,$from,$key);
  40. if($unique_table==1){
  41. return $cipher;
  42. }else{
  43. $table_error="error: Same substitution table detected:\n";
  44. for($i=0;$i<strlen($detected_table);$i=$i+2){
  45. $d1=substr($detected_table,$i,1);
  46. $d2=substr($detected_table,$i+1,1);
  47. $table_error=$table_error."table ".$d1." and table ".$d2. "\n";
  48. }
  49. return $table_error;
  50. }
  51. }
  52. top();
  53. echo "<form name=\"Form\" action=\"mono.php\" method=\"post\">\n";
  54. echo "Enter text you want to encode or decode:<br>\n";
  55. echo "<font size=-1>[- note: lowercase characters and no space -]</font><br>\n";
  56. echo "<textarea name=\"plain\" cols=40 rows=7 class=\"txtcolor\">\n";
  57. if ($plain){
  58. $plain=filter($plain);
  59. $plain=check_length($plain);
  60. echo $plain;
  61. }
  62. echo "</textarea><br><br>\n";
  63. echo "<table border=1 cellspacing=0 bordercolor=\"#000000\">\n";
  64. echo "<th colspan=13>Substitution Table :</th>\n";
  65. echo "<tr>\n";
  66. for($i=0;$i<=12;$i++){
  67. echo "<td align=center>".ascii_letter($i)."</td>\n";
  68. }
  69. echo "</tr><tr>\n";
  70. for($i=0;$i<=12;$i++){
  71. echo "<td>\n";
  72. echo "<select name=\"key[$i]\" width=200 class=\"txtcolor\">\n";
  73. echo "<option>--</option>\n";
  74. for($j=0;$j<=25;$j++){
  75. echo "<option value=".ascii_letter($j).">".ascii_letter($j)."</option>\n";
  76. }
  77. if (ascii_letter($j)==$key[$i]){
  78. echo "<option value=".$key[$i]." selected>".$key[$i]."</option>\n";
  79. }
  80. echo "</select>\n";
  81. echo "</td>\n";
  82. }
  83. echo "</tr><tr>\n";
  84. for($i=13;$i<=25;$i++){
  85. echo "<td align=center>".ascii_letter($i)."</td>\n";
  86. }
  87. echo "</tr><tr>\n";
  88. for($i=13;$i<=25;$i++){
  89. echo "<td>\n";
  90. echo "<select name=\"key[$i]\" width=200 class=\"txtcolor\">\n";
  91. echo "<option>--</option>\n";
  92. for($j=0;$j<=25;$j++){
  93. echo "<option value=".ascii_letter($j).">".ascii_letter($j)."</option>\n";
  94. }
  95. if (ascii_letter($j)==$key[$i]){
  96. echo "<option value=".$key[$i]." selected>".$key[$i]."</option>\n";
  97. }
  98. echo "</select>\n";
  99. echo "</td>\n";
  100. }
  101. echo "</tr>\n";
  102. echo "</table>\n";
  103. echo "<br><br>";
  104. echo "<input type=\"submit\" name=\"process\" value=\"Encode/Decode\" class=\"txtcolor\">\n";
  105. echo "</form>\n";
  106. if ($process=="Encode/Decode"){
  107. $key_table="";
  108. for($i=0;$i<=25;$i++){
  109. $key_table=$key_table.$key[$i];
  110. }
  111. $cipher=mono($plain,$key_table);
  112. echo "Text after encoding or decoding:<br>\n";
  113. echo "<textarea cols=40 rows=7 class=\"txtcolor\">$cipher</textarea><br><br>\n";
  114. }
  115. bottom();
  116. ?>

comments powered by Disqus