-
Notifications
You must be signed in to change notification settings - Fork 0
/
Appendix C.php
96 lines (81 loc) · 2.08 KB
/
Appendix C.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
<?php
/**
* Created by PhpStorm.
* User: User
* Date: 31.01.2016
* Time: 17:06
*/
function getPermutations($key = '', $max = 0, $depth = 0, $row = array()) {
$perms = array();
for($i = 0; $i <= $max; ++$i) {
$row[$depth] = isset($row[$depth]) ? ($row[$depth] + 1) : 0;
if($depth < $max) {
$res = getPermutations($key, $max, $depth + 1, $row);
foreach($res as $subrow)
$perms[] = $subrow;
} else {
$subrow = '';
foreach($row as $char)
$subrow .= $key{$char};
$perms[] = $subrow;
}
}
return($perms);
}
function getMap($key){
$alphabet = getPermutations($key, strlen($key) - 1);
$index=0;
for($i=65;$i<92;$i++){
$map[chr($i)] = $alphabet[$index];
$index++;
}
return $map;
}
function decode($map,$msg_in)
{
$msg_index=0;$map_index=0;
foreach($msg_in as $char_in){
foreach($map as $char_out => $value){
if($char_in == $char_out) {
$msg_out[$msg_index] = $value;
}
$map_index++;
}
$msg_index++;
}
return $msg_out;
}
function encode($map,$msg_in){
$msg_index=0;$map_index=0;
foreach($msg_in as $char_in){
foreach($map as $char_out => $value){
if($char_in == $value) {
$msg_out[$msg_index] = $char_out;
}
$map_index++;
}
$msg_index++;
}
return $msg_out;
}
$key = './:';
if( (strlen($key) == 3) and (strlen(implode("",array_unique(str_split($key)))) == 3) ){
$map = getMap($key);
$plaintext = str_split('NOTHINGELSEMATTER');
echo "PLAIN TEXT: \n";
foreach($plaintext as $char){
echo $char." ";
}
$decoded = decode($map,$plaintext);
echo "\n DECODED: \n";
foreach($decoded as $char){
echo $char." ";
}
$encoded = encode($map,$decoded);
echo "\n ENCODED: \n";
foreach($encoded as $char){
echo $char." ";
}
}else{
printf("Key size should be 3 unrepeteable symbols. Please check your key");
}