1
1
<?php
2
- namespace amculin \encryption ;
2
+ namespace amculin \cryptography \ classic ;
3
3
4
4
/**
5
5
* This file is the main class for vigenere cipher encryption algortithm
6
- *
6
+ *
7
7
* @author Fahmi Auliya Tsani <[email protected] >
8
- * @version 0.1
8
+ * @version 0.2
9
9
*/
10
10
11
11
class VigenereCipher
@@ -18,12 +18,33 @@ class VigenereCipher
18
18
*/
19
19
const TABULA_RECTA = 'abcdefghijklmnopqrstuvwxyz ' ;
20
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
+
21
42
/**
22
43
* The plain text/message to be encrypted
23
44
*
24
45
* @var string
25
46
*/
26
- public $ plain_text ;
47
+ public $ plainText ;
27
48
28
49
/**
29
50
* The key used to encrypt plain text/message
@@ -33,32 +54,132 @@ class VigenereCipher
33
54
public $ key ;
34
55
35
56
/**
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
37
159
*
38
160
* @param string message
39
161
* @return void
40
162
*/
41
- public function setPlainText ( $ message )
163
+ public function setCipherText ( string $ cipherText ): void
42
164
{
43
- $ this ->plain_text = $ message ;
165
+ $ this ->cipherText = $ cipherText ;
44
166
}
45
167
46
168
/**
47
- * Set key
169
+ * Method to generate the key
48
170
* We loop the key then concatenate it until it has the same length with the plain text
49
171
* Example:
50
172
* Plain text: vigenerecipher (14 characters)
51
173
* Key: abcd (4 characters)
52
174
* Repeated key: abcdabcdabcdab (14 characters)
53
175
*
54
176
* @param string key
55
- * @return void
177
+ * @return string
56
178
*/
57
- public function setKey ( $ key )
179
+ public function generateKey ( string $ key ): string
58
180
{
59
- $ plainTextLength = strlen ($ this ->plain_text );
60
181
$ keyLength = strlen ($ key );
61
- $ messageLength = strlen ($ this ->plain_text );
182
+ $ messageLength = strlen ($ this ->mode == $ this :: ENCRYPT_MODE ? $ this -> plainText : $ this -> cipherText );
62
183
63
184
$ repeatTimes = floor ($ messageLength / $ keyLength );
64
185
$ paddingKeyLength = $ messageLength - ($ keyLength * $ repeatTimes );
@@ -69,61 +190,51 @@ public function setKey($key)
69
190
$ repeatedKey .= $ key ;
70
191
}
71
192
72
- $ paddedKey = $ repeatedKey . substr ($ key , 0 , $ paddingKeyLength );
73
-
74
- $ this ->key = $ paddedKey ;
193
+ return $ repeatedKey . substr ($ key , 0 , $ paddingKeyLength );
75
194
}
76
195
77
196
/**
78
197
* Method to encrypt the plain text
79
198
*
80
- * @return string
199
+ * @return void
81
200
*/
82
- public function encrypt (): string
201
+ public function encrypt (): void
83
202
{
84
- $ messageLength = strlen ($ this ->plain_text );
203
+ $ messageLength = strlen ($ this ->plainText );
85
204
$ cipher = '' ;
86
205
87
206
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 ));
89
208
$ keyCharPosition = strpos (self ::TABULA_RECTA , substr ($ this ->key , $ i , 1 ));
90
209
91
210
$ shift = $ messageCharPosition + $ keyCharPosition ;
92
211
$ cipherCharPosition = $ shift % strlen (self ::TABULA_RECTA );
93
212
$ cipher .= substr (self ::TABULA_RECTA , $ cipherCharPosition , 1 );
94
213
}
95
-
96
- return $ cipher ;
97
- }
98
214
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 );
107
216
}
108
217
109
218
/**
110
- * Method to get key
219
+ * Method to decrypt the cipher text
111
220
*
112
- * @return string
221
+ * @return void
113
222
*/
114
- public function getKey (): string
223
+ public function decrypt (): void
115
224
{
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 );
118
234
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 );
127
239
}
128
240
}
129
- ?>
0 commit comments