|
1 | 1 | <?php
|
2 | 2 | namespace amculin\cryptography\classic;
|
3 | 3 |
|
4 |
| -/** |
5 |
| - * This file is the main class for vigenere cipher encryption algortithm |
6 |
| - * |
7 |
| - * @author Fahmi Auliya Tsani <[email protected]> |
8 |
| - * @version 0.2 |
9 |
| - */ |
| 4 | +use amculin\cryptography\classic\enums\ProcessType; |
| 5 | +use amculin\cryptography\classic\enums\VigenereMode; |
10 | 6 |
|
11 | 7 | class VigenereCipher
|
12 | 8 | {
|
13 |
| - /** |
14 |
| - * Default list of acceptable character to be encrypted using vigenere |
15 |
| - * By default, it just an alphabetical list. |
16 |
| - * |
17 |
| - * @var string |
18 |
| - */ |
19 |
| - const TABULA_RECTA = 'abcdefghijklmnopqrstuvwxyz'; |
20 |
| - |
21 |
| - /** |
22 |
| - * Mode for encryption process |
23 |
| - * |
24 |
| - * @var string |
25 |
| - */ |
26 |
| - const ENCRYPT_MODE = 'encrypt'; |
27 |
| - |
28 |
| - /** |
29 |
| - * Mode for decryption process |
30 |
| - * |
31 |
| - * @var string |
32 |
| - */ |
33 |
| - const DECRYPT_MODE = 'decrypt'; |
34 |
| - |
35 |
| - /** |
36 |
| - * The mode of current process whether it is encrypt mode or decrypt mode |
37 |
| - * |
38 |
| - * @var string |
39 |
| - */ |
40 |
| - public $mode; |
41 |
| - |
42 |
| - /** |
43 |
| - * The plain text/message to be encrypted |
44 |
| - * |
45 |
| - * @var string |
46 |
| - */ |
47 |
| - public $plainText; |
48 |
| - |
49 |
| - /** |
50 |
| - * The key used to encrypt plain text/message |
51 |
| - * |
52 |
| - * @var string |
53 |
| - */ |
54 |
| - public $key; |
55 |
| - |
56 |
| - /** |
57 |
| - * The cipher text to be decrypted |
58 |
| - * |
59 |
| - * @var string |
60 |
| - */ |
61 |
| - public $cipherText; |
62 |
| - |
63 |
| - public function __construct(string $mode = 'encrypt', string $data = null, string $key = null) |
| 9 | + public static function getClassName(string $mode): string |
64 | 10 | {
|
65 |
| - $this->setMode($mode); |
| 11 | + $path = 'amculin\cryptography\classic\\'; |
66 | 12 |
|
67 |
| - if ($mode == $this::ENCRYPT_MODE) { |
68 |
| - if (! is_null($data) && ! is_null($key)) { |
69 |
| - $this->setPlainText($data); |
70 |
| - $this->setKey($key); |
71 |
| - $this->encrypt(); |
72 |
| - } |
73 |
| - } else { |
74 |
| - if (! is_null($data) && ! is_null($key)) { |
75 |
| - $this->setCipherText($data); |
76 |
| - $this->setKey($key); |
77 |
| - $this->decrypt(); |
78 |
| - } |
| 13 | + if ($mode == VigenereMode::BASIC->value) { |
| 14 | + return $path . 'BasicVigenereCipher'; |
| 15 | + } elseif ($mode == VigenereMode::ALPHA_NUMERIC->value) { |
| 16 | + return $path . 'AlnumVigenereCipher'; |
79 | 17 | }
|
80 | 18 | }
|
81 | 19 |
|
82 |
| - /** |
83 |
| - * Method to get current mode |
84 |
| - * |
85 |
| - * @return string |
86 |
| - */ |
87 |
| - public function getMode(): string |
88 |
| - { |
89 |
| - return $this->mode; |
90 |
| - } |
91 |
| - |
92 |
| - /** |
93 |
| - * Set the current mode |
94 |
| - * |
95 |
| - * @param string mode |
96 |
| - * @return void |
97 |
| - */ |
98 |
| - public function setMode(string $mode): void |
| 20 | + public static function encrypt(string $data, string $key, string $mode = 'basic'): string |
99 | 21 | {
|
100 |
| - $this->mode = $mode; |
101 |
| - } |
| 22 | + $className = self::getClassName($mode); |
102 | 23 |
|
103 |
| - /** |
104 |
| - * Method to get plain text |
105 |
| - * |
106 |
| - * @return string |
107 |
| - */ |
108 |
| - public function getPlainText(): string |
109 |
| - { |
110 |
| - return $this->plainText; |
111 |
| - } |
| 24 | + $processName = ProcessType::ENCRYPT->value; |
112 | 25 |
|
113 |
| - /** |
114 |
| - * Set the plain text/message/data to be encrypted |
115 |
| - * |
116 |
| - * @param string message |
117 |
| - * @return void |
118 |
| - */ |
119 |
| - public function setPlainText(string $plainText): void |
120 |
| - { |
121 |
| - $this->plainText = $plainText; |
122 |
| - } |
| 26 | + $encrypt = new $className($processName, $data, $key); |
123 | 27 |
|
124 |
| - /** |
125 |
| - * Method to get key |
126 |
| - * |
127 |
| - * @return string |
128 |
| - */ |
129 |
| - public function getKey(): string |
130 |
| - { |
131 |
| - return $this->key; |
| 28 | + return $encrypt->getCipherText(); |
132 | 29 | }
|
133 | 30 |
|
134 |
| - /** |
135 |
| - * Set the key to be be used in encryption/decryption process |
136 |
| - * |
137 |
| - * @param string key |
138 |
| - * @return void |
139 |
| - */ |
140 |
| - public function setKey(string $key): void |
| 31 | + public static function decrypt(string $data, string $key, string $mode = 'basic'): string |
141 | 32 | {
|
142 |
| - $paddedKey = $this->generateKey($key); |
| 33 | + $className = self::getClassName($mode); |
143 | 34 |
|
144 |
| - $this->key = $paddedKey; |
145 |
| - } |
| 35 | + $processName = ProcessType::DECRYPT->value; |
146 | 36 |
|
147 |
| - /** |
148 |
| - * Method to get cipher text |
149 |
| - * |
150 |
| - * @return string |
151 |
| - */ |
152 |
| - public function getCipherText(): string |
153 |
| - { |
154 |
| - return $this->cipherText; |
155 |
| - } |
| 37 | + $decrypt = new $className($processName, $data, $key); |
156 | 38 |
|
157 |
| - /** |
158 |
| - * Set the cipher text result from encryption process |
159 |
| - * |
160 |
| - * @param string message |
161 |
| - * @return void |
162 |
| - */ |
163 |
| - public function setCipherText(string $cipherText): void |
164 |
| - { |
165 |
| - $this->cipherText = $cipherText; |
166 |
| - } |
167 |
| - |
168 |
| - /** |
169 |
| - * Method to generate the key |
170 |
| - * We loop the key then concatenate it until it has the same length with the plain text |
171 |
| - * Example: |
172 |
| - * Plain text: vigenerecipher (14 characters) |
173 |
| - * Key: abcd (4 characters) |
174 |
| - * Repeated key: abcdabcdabcdab (14 characters) |
175 |
| - * |
176 |
| - * @param string key |
177 |
| - * @return string |
178 |
| - */ |
179 |
| - public function generateKey(string $key): string |
180 |
| - { |
181 |
| - $keyLength = strlen($key); |
182 |
| - $messageLength = strlen($this->mode == $this::ENCRYPT_MODE ? $this->plainText : $this->cipherText); |
183 |
| - |
184 |
| - $repeatTimes = floor($messageLength / $keyLength); |
185 |
| - $paddingKeyLength = $messageLength - ($keyLength * $repeatTimes); |
186 |
| - |
187 |
| - $repeatedKey = ''; |
188 |
| - |
189 |
| - for ($i = 0; $i < $repeatTimes; $i++) { |
190 |
| - $repeatedKey .= $key; |
191 |
| - } |
192 |
| - |
193 |
| - return $repeatedKey . substr($key, 0, $paddingKeyLength); |
194 |
| - } |
195 |
| - |
196 |
| - /** |
197 |
| - * Method to encrypt the plain text |
198 |
| - * |
199 |
| - * @return void |
200 |
| - */ |
201 |
| - public function encrypt(): void |
202 |
| - { |
203 |
| - $messageLength = strlen($this->plainText); |
204 |
| - $cipher = ''; |
205 |
| - |
206 |
| - for ($i = 0; $i < $messageLength; $i++) { |
207 |
| - $messageCharPosition = strpos(self::TABULA_RECTA, substr($this->plainText, $i, 1)); |
208 |
| - $keyCharPosition = strpos(self::TABULA_RECTA, substr($this->key, $i, 1)); |
209 |
| - |
210 |
| - $shift = $messageCharPosition + $keyCharPosition; |
211 |
| - $cipherCharPosition = $shift % strlen(self::TABULA_RECTA); |
212 |
| - $cipher .= substr(self::TABULA_RECTA, $cipherCharPosition, 1); |
213 |
| - } |
214 |
| - |
215 |
| - $this->setCipherText($cipher); |
216 |
| - } |
217 |
| - |
218 |
| - /** |
219 |
| - * Method to decrypt the cipher text |
220 |
| - * |
221 |
| - * @return void |
222 |
| - */ |
223 |
| - public function decrypt(): void |
224 |
| - { |
225 |
| - $messageLength = strlen($this->cipherText); |
226 |
| - $plain = ''; |
227 |
| - |
228 |
| - for ($i = 0; $i < $messageLength; $i++) { |
229 |
| - $messageCharPosition = strpos(self::TABULA_RECTA, substr($this->cipherText, $i, 1)); |
230 |
| - $keyCharPosition = strpos(self::TABULA_RECTA, substr($this->key, $i, 1)); |
231 |
| - |
232 |
| - $shift = $messageCharPosition - $keyCharPosition; |
233 |
| - $plainCharPosition = $shift % strlen(self::TABULA_RECTA); |
234 |
| - |
235 |
| - $plain .= substr(self::TABULA_RECTA, $plainCharPosition, 1); |
236 |
| - } |
237 |
| - |
238 |
| - $this->setPlainText($plain); |
| 39 | + return $decrypt->getPlainText(); |
239 | 40 | }
|
240 | 41 | }
|
0 commit comments