Skip to content

Commit 2f54c8e

Browse files
committed
[DEV] Refactor the main class
1 parent 64fc1de commit 2f54c8e

File tree

1 file changed

+155
-44
lines changed

1 file changed

+155
-44
lines changed

source/VigenereCipher.php

Lines changed: 155 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
2-
namespace amculin\encryption;
2+
namespace amculin\cryptography\classic;
33

44
/**
55
* This file is the main class for vigenere cipher encryption algortithm
6-
*
6+
*
77
* @author Fahmi Auliya Tsani <[email protected]>
8-
* @version 0.1
8+
* @version 0.2
99
*/
1010

1111
class VigenereCipher
@@ -18,12 +18,33 @@ class VigenereCipher
1818
*/
1919
const TABULA_RECTA = 'abcdefghijklmnopqrstuvwxyz';
2020

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+
2142
/**
2243
* The plain text/message to be encrypted
2344
*
2445
* @var string
2546
*/
26-
public $plain_text;
47+
public $plainText;
2748

2849
/**
2950
* The key used to encrypt plain text/message
@@ -33,32 +54,132 @@ class VigenereCipher
3354
public $key;
3455

3556
/**
36-
* Set the plain text/message to be encrypted
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)
64+
{
65+
$this->setMode($mode);
66+
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+
}
79+
}
80+
}
81+
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
99+
{
100+
$this->mode = $mode;
101+
}
102+
103+
/**
104+
* Method to get plain text
105+
*
106+
* @return string
107+
*/
108+
public function getPlainText(): string
109+
{
110+
return $this->plainText;
111+
}
112+
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+
}
123+
124+
/**
125+
* Method to get key
126+
*
127+
* @return string
128+
*/
129+
public function getKey(): string
130+
{
131+
return $this->key;
132+
}
133+
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
141+
{
142+
$paddedKey = $this->generateKey($key);
143+
144+
$this->key = $paddedKey;
145+
}
146+
147+
/**
148+
* Method to get cipher text
149+
*
150+
* @return string
151+
*/
152+
public function getCipherText(): string
153+
{
154+
return $this->cipherText;
155+
}
156+
157+
/**
158+
* Set the cipher text result from encryption process
37159
*
38160
* @param string message
39161
* @return void
40162
*/
41-
public function setPlainText($message)
163+
public function setCipherText(string $cipherText): void
42164
{
43-
$this->plain_text = $message;
165+
$this->cipherText = $cipherText;
44166
}
45167

46168
/**
47-
* Set key
169+
* Method to generate the key
48170
* We loop the key then concatenate it until it has the same length with the plain text
49171
* Example:
50172
* Plain text: vigenerecipher (14 characters)
51173
* Key: abcd (4 characters)
52174
* Repeated key: abcdabcdabcdab (14 characters)
53175
*
54176
* @param string key
55-
* @return void
177+
* @return string
56178
*/
57-
public function setKey($key)
179+
public function generateKey(string $key): string
58180
{
59-
$plainTextLength = strlen($this->plain_text);
60181
$keyLength = strlen($key);
61-
$messageLength = strlen($this->plain_text);
182+
$messageLength = strlen($this->mode == $this::ENCRYPT_MODE ? $this->plainText : $this->cipherText);
62183

63184
$repeatTimes = floor($messageLength / $keyLength);
64185
$paddingKeyLength = $messageLength - ($keyLength * $repeatTimes);
@@ -69,61 +190,51 @@ public function setKey($key)
69190
$repeatedKey .= $key;
70191
}
71192

72-
$paddedKey = $repeatedKey . substr($key, 0, $paddingKeyLength);
73-
74-
$this->key = $paddedKey;
193+
return $repeatedKey . substr($key, 0, $paddingKeyLength);
75194
}
76195

77196
/**
78197
* Method to encrypt the plain text
79198
*
80-
* @return string
199+
* @return void
81200
*/
82-
public function encrypt(): string
201+
public function encrypt(): void
83202
{
84-
$messageLength = strlen($this->plain_text);
203+
$messageLength = strlen($this->plainText);
85204
$cipher = '';
86205

87206
for ($i = 0; $i < $messageLength; $i++) {
88-
$messageCharPosition = strpos(self::TABULA_RECTA, substr($this->plain_text, $i, 1));
207+
$messageCharPosition = strpos(self::TABULA_RECTA, substr($this->plainText, $i, 1));
89208
$keyCharPosition = strpos(self::TABULA_RECTA, substr($this->key, $i, 1));
90209

91210
$shift = $messageCharPosition + $keyCharPosition;
92211
$cipherCharPosition = $shift % strlen(self::TABULA_RECTA);
93212
$cipher .= substr(self::TABULA_RECTA, $cipherCharPosition, 1);
94213
}
95-
96-
return $cipher;
97-
}
98214

99-
/**
100-
* Method to get plain text
101-
*
102-
* @return string
103-
*/
104-
public function getPlainText(): string
105-
{
106-
return $this->plain_text;
215+
$this->setCipherText($cipher);
107216
}
108217

109218
/**
110-
* Method to get key
219+
* Method to decrypt the cipher text
111220
*
112-
* @return string
221+
* @return void
113222
*/
114-
public function getKey(): string
223+
public function decrypt(): void
115224
{
116-
return $this->key;
117-
}
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);
118234

119-
/**
120-
* Method to get cipher text
121-
*
122-
* @return string
123-
*/
124-
public function getCipherText(): string
125-
{
126-
return $this->encrypt();
235+
$plain .= substr(self::TABULA_RECTA, $plainCharPosition, 1);
236+
}
237+
238+
$this->setPlainText($plain);
127239
}
128240
}
129-
?>

0 commit comments

Comments
 (0)