-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrasposition.php
136 lines (135 loc) · 4.25 KB
/
trasposition.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
##
## This code is licensed under the GPL-v3
## Belongs to the "encrypt" system that can be found here:
## https://github.com/CaeSpock/encrypt
## Questions and suggestions are always welcomed. Send me an E-Mail at: CAnibarro<at>WhiteSith<dot>com
##
// Trasposition cipher
// https://en.wikipedia.org/wiki/Transposition_cipher
if ($enckey==0 || is_null($enckey)) {
echo "<div class=\"alert alert-danger\">\n";
echo "<strong>$l_alert!</strong> $l_noksent\n";
echo "</div>\n";
} else {
if ($phrase == "" or is_null($phrase)) {
echo "<div class=\"alert alert-danger\">\n";
echo "<strong>$l_alert!</strong> $l_nopsent\n";
echo "</div>\n";
} else {
echo "<div class=\"table-responsive\">\n";
echo "<table class=\"table table-sm\">\n";
echo "<tr>\n";
echo " <td class=\"table-dark text-right\">$l_type:</td>\n";
echo " <td>$l_trasposition - K=$enckey</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo " <td class=\"bg-warning text-right\"><strong>$l_phrase:</strong></td>\n";
echo " <td><samp>$phrase</samp></td>";
echo "</tr>\n";
echo "<tr>\n";
echo " <td class=\"bg-info text-right\"><strong>$l_matrix:</strong></td>\n";
echo " <td><samp><pre>|";
$position = 1;
while ($position <= $enckey) {
echo "$position|";
$position++;
}
echo "\n-";
$position = 1;
while ($position <= $enckey) {
echo "--";
$position++;
}
echo "\n";
while ((strlen($phrase) % $enckey) != 0) {
$phrase .= " ";
}
$position = 0;
$counter = 1;
$newphrase = array();
$row = 1;
while ($position < strlen($phrase)) {
echo "|".$phrase[$position];
$column=$counter;
$prearray = array("row"=>$row,"column"=>$column, "letter"=>$phrase[$position]);
array_push($newphrase, $prearray);
$position++;
$counter++;
if ($counter > $enckey) {
echo "|\n";
$counter = 1;
$row++;
}
}
echo "</pre></samp></td>";
echo "</tr>\n";
echo "<tr>\n";
echo " <td class=\"bg-success text-right\"><strong>$l_newphrase:</strong></td>\n";
echo " <td><pre>";
$columns = $enckey;
while ($columns >= 1) {
$rows = 1;
while ($rows <= $row) {
$newcounter = 0;
while ($newcounter <count($newphrase)) {
if ($newphrase[$newcounter]["row"]==$rows and $newphrase[$newcounter]["column"]==$columns) {
echo $newphrase[$newcounter]["letter"];
}
$newcounter++;
}
$rows++;
}
$columns--;
}
echo "</pre></td>\n";
echo "</tr>\n";
while ((strlen($phrase) % $enckey) != 0) {
$phrase .= " ";
}
$position = 0;
$newphrase = array();
$row = 1;
$column = $enckey;
$maxrows = strlen($phrase)/$enckey;
while ($position < strlen($phrase)) {
$prearray = array("row"=>$row,"column"=>$column, "letter"=>$phrase[$position]);
array_push($newphrase, $prearray);
$row++;
$position++;
if ($row>$maxrows) {
$row=1;
$column--;
}
}
echo "<tr>\n";
echo " <td class=\"bg-danger text-right\"><strong>$l_inverted:</strong></td>\n";
echo " <td><pre>";
$rows = 1;
$columns = 1;
$counter = 0;
while ($counter < strlen($phrase)) {
$newcounter = 0;
while ($newcounter <count($newphrase)) {
if ($newphrase[$newcounter]["row"]==$rows and $newphrase[$newcounter]["column"]==$columns) {
echo $newphrase[$newcounter]["letter"];
}
$newcounter++;
}
$columns++;
if ($columns>$enckey) {
$rows++;
$columns=1;
}
$counter++;
}
echo "</pre></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</div>\n";
}
}
echo "<br /><br />";
echo "<div class=\"text text-center\">\n";
echo "<a href=\"$phpself\" class=\"btn btn-secondary\" role=\"button\">$l_goback</a>\n";
echo "</div>";