-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_transposition.php
187 lines (151 loc) · 5.15 KB
/
double_transposition.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* User: Phu Tran
* Date: 5/6/2019
*/
//
function dt_cipher_encryption($key, $plaintext) {
/**
* Using double transposition cipher to encrypt a plaintext
* @param: $key (int) - the key that use to encrypt or decrypt
* @param: $plaintext (string) - the message that is being encrypt or decrypt
* @return: (array) - array[0] -> hold the (string) the ciphertext
* array[1] -> hold the (array) the random order of integer of rows being shuffle
* array[2] -> hold the (array) the random order of integer of columns being shuffle
*/
// set up the necessary information
$tmp_ary = setup_helper($key, $plaintext);
$ary = $tmp_ary[0];
$row = $tmp_ary[1];
$col = $tmp_ary[2];
// create a 2-dimension array with * as default
$new_ary = array(array());
// shuffle row
$row_range = range(0, $row-1);
shuffle($row_range);
// swap/rearrange the row
$counter = 0;
foreach($row_range as $index) {
$new_ary[$counter] = $ary[$index];
$counter++;
}
// shuffle col
$col_range = range(0, $col-1);
shuffle($col_range);
$final_ary = $new_ary;
// swap/rearrange the column
$counter = 0;
foreach ($col_range as $index) {
for($i=0; $i<$row; $i++) {
$final_ary[$i][$counter] = $new_ary[$i][$index];
}
$counter++;
}
// convert 2D array back to string
$ciphertext = "";
for($i=0; $i<$row; $i++) {
for($k=0; $k<$col; $k++) {
$ciphertext .= $final_ary[$i][$k];
}
}
return array($ciphertext, $row_range, $col_range);
}
//no space allow
function dt_cipher_decryption($key, $ciphertext, $row_key, $col_key) {
/**
* Using double transposition cipher to decrypt a ciphertext
* @param: $key (int) - the key that use to encrypt or decrypt
* @param: $ciphertext (string) - the message that is being encrypt or decrypt
* @param: $row_key (array) - hold the array the random order of integer of rows that being shuffle
* @param: $col_key (array) - hold the array the random order of integer of columns that being shuffle
* @return: (string) - plaintext if correct
* (boolean) - if false
*/
// set up the necessary information
$tmp_ary = setup_helper($key, $ciphertext);
$ary = $tmp_ary[0];
$row = $tmp_ary[1];
$col = $tmp_ary[2];
//check if row or column key have the same number as row and column in array
if(max($row_key)+1 != $row or max($col_key)+1 != $col)
return false;
//check if the size of the 2d array is the same as the key row and col
if(count($ary) != count($row_key) or count($ary[0]) != count($col_key))
return false;
$final_ary = $ary;
$r_index = 0;
$c_index = 0;
foreach ($row_key as $r) {
foreach ($col_key as $c) {
$final_ary[$r][$c] = $ary[$r_index][$c_index];
$c_index++;
}
$c_index = 0;
$r_index++;
}
// convert 2D array back to string
$plaintext = "";
for($i=0; $i<$row; $i++) {
for($k=0; $k<$col; $k++) {
if ($final_ary[$i][$k] == "*")
$plaintext .= ' ';
else
$plaintext .= $final_ary[$i][$k];
}
}
return $plaintext;
}
function setup_helper($key, $message) {
/**
* Help setup the 2D array for encrypting and decrypting
* @param: $key (int) - the key that use to encrypt or decrypt
* @param: $message (string) - the message that is being encrypt or decrypt
* @return: (array) - array[0] -> hold the (2D array) of the message where column is based on the $key size
* array[1] -> hold the (int) represent # of rows
* array[2] -> hold the (int) represent # of columns
*/
// set up the value of row and column
$col = $key;
$row = floor(strlen($message)/$col);
if (strlen($message) % $col != 0)
$row += 1;
// create a 2-dimension array with * as default
$ary = array_fill(0, $row, array_fill(0, $col, '*'));
// put message inside the array
$counter = 0;
for ($i=0; $i<$row; $i++) {
for($k=0; $k<$col; $k++) {
if($counter < strlen($message) and $message[$counter] != ' ')
$ary[$i][$k] = $message[$counter];
$counter++;
}
}
return array($ary, $row, $col);
}
//function main() {
// $mykey = 3;
// $plaintext = "hello world 4231";
//
//
// $list = dt_cipher_encryption($mykey, $plaintext);
//
// $cipher = $list[0];
// $dkey_row = $list[1];
// $dkey_col = $list[2];
//
// echo 'decrypt row_key is: ';
// foreach ($dkey_row as $i) {
// echo $i;
// } echo "\n";
//
// echo 'decrypt col_key is: ';
// foreach ($dkey_col as $i) {
// echo $i;
// } echo "\n";
//
// echo "this is the cipher: $cipher\n\n";
// echo dt_cipher_decryption($mykey, $cipher, $dkey_row, $dkey_col);
//
//}
//main();
?>