Skip to content

Commit 5df03ae

Browse files
committed
[DEV] Implement Factory Pattern, Support Alpha-Numeric mode;
1 parent 2f54c8e commit 5df03ae

File tree

5 files changed

+71
-217
lines changed

5 files changed

+71
-217
lines changed

source/AlnumVigenereCipher.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace amculin\cryptography\classic;
3+
4+
/**
5+
* This file is the main class for alpha-numric mode vigenere cipher algortithm
6+
*
7+
* @author Fahmi Auliya Tsani <[email protected]>
8+
* @version 0.1
9+
*/
10+
11+
class AlnumVigenereCipher extends VigenereCipherBlueprint
12+
{
13+
/**
14+
* Default list of acceptable character to be used in vigenere cipher algorithm
15+
* This list cointains alpha-numeric characters including the capitalized
16+
*
17+
* @var string
18+
*/
19+
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
20+
}

source/BasicVigenereCipher.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace amculin\cryptography\classic;
3+
4+
/**
5+
* This file is the main class for basic vigenere cipher algortithm
6+
*
7+
* @author Fahmi Auliya Tsani <[email protected]>
8+
* @version 0.2
9+
*/
10+
11+
class BasicVigenereCipher extends VigenereCipherBlueprint
12+
{
13+
/**
14+
* @inheritdoc
15+
*/
16+
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
17+
}

source/VigenereCipher.php

Lines changed: 18 additions & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -1,240 +1,41 @@
11
<?php
22
namespace amculin\cryptography\classic;
33

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;
106

117
class VigenereCipher
128
{
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
6410
{
65-
$this->setMode($mode);
11+
$path = 'amculin\cryptography\classic\\';
6612

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';
7917
}
8018
}
8119

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
9921
{
100-
$this->mode = $mode;
101-
}
22+
$className = self::getClassName($mode);
10223

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;
11225

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);
12327

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();
13229
}
13330

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
14132
{
142-
$paddedKey = $this->generateKey($key);
33+
$className = self::getClassName($mode);
14334

144-
$this->key = $paddedKey;
145-
}
35+
$processName = ProcessType::DECRYPT->value;
14636

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);
15638

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();
23940
}
24041
}

source/enums/ProcessType.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace amculin\cryptography\classic\enums;
3+
4+
enum ProcessType: string {
5+
case ENCRYPT = 'encrypt';
6+
case DECRYPT = 'decrypt';
7+
}

source/enums/VigenereMode.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace amculin\cryptography\classic\enums;
3+
4+
enum VigenereMode: string {
5+
case BASIC = 'basic';
6+
case ALPHA_NUMERIC = 'alpha_numeric';
7+
case BASE64 = 'base64';
8+
case ASCII = 'ascii';
9+
}

0 commit comments

Comments
 (0)