-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.php
100 lines (85 loc) · 2.54 KB
/
control.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
<?php
include 'backend/cipher.php';
include 'backend/caesar.php';
include 'backend/vigenere.php';
include 'backend/columnar.php';
function encryptChoice ($choiceCipher, $input, $key, $choiceCrypt) {
if (empty($input) && empty($key)) {
return "";
}
if (!validateInput($input)) {
return "Invalid input. Please enter only alphabetic characters.";
}
switch ($choiceCipher) {
case 'caesar':
if (!validateKey_Caesar($key)) {
return "Invalid key. Please enter only numeric characters.";
}
$cipher = new Caesar($input, $key);
if ($choiceCrypt == "encrypt") {
$cipher->encrypt();
} else {
$cipher->decrypt();
}
return $cipher->getOutput();
break;
case 'vigenere':
if (!validateKey_String($key)) {
return "Invalid key. Please enter only alphabetic characters.";
}
$cipher = new Vigenere($input, $key);
if ($choiceCrypt == "encrypt") {
$cipher->encrypt();
} else {
$cipher->decrypt();
}
return $cipher->getOutput();
break;
case 'columnar':
if (!validateKey($key)) {
return "Invalid key. Please enter a key.";
}
$cipher = new Columnar($input, $key);
if ($choiceCrypt == "encrypt") {
$cipher->encrypt();
} else {
$cipher->decrypt();
}
return $cipher->getOutput();
break;
}
}
function validateInput ($input) {
if (empty($input)) {
return false;
}
if (!ctype_alpha(str_replace(' ', '', $input))) {
return false;
}
return true;
}
function validateKey($key) {
if (empty($key)) {
return false;
}
return true;
}
function validateKey_String ($key) {
if (empty($key)) {
return false;
}
if (!ctype_alpha($key)) {
return false;
}
return true;
}
function validateKey_Caesar ($key) {
if (empty($key)) {
return false;
}
if (!ctype_digit($key)) {
return false;
}
return true;
}
?>