11<?php
2- namespace amculin \encryption ;
2+ 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.1
9- */
4+ use amculin \cryptography \classic \enums \ProcessType ;
5+ use amculin \cryptography \classic \enums \VigenereMode ;
106
117class 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- * The plain text/message to be encrypted
23- *
24- * @var string
25- */
26- public $ plain_text ;
27-
28- /**
29- * The key used to encrypt plain text/message
30- *
31- * @var string
32- */
33- public $ key ;
34-
35- /**
36- * Set the plain text/message to be encrypted
37- *
38- * @param string message
39- * @return void
40- */
41- public function setPlainText ($ message )
9+ public static function getClassName (string $ mode ): string
4210 {
43- $ this ->plain_text = $ message ;
11+ $ path = 'amculin\cryptography\classic \\' ;
12+
13+ if ($ mode == VigenereMode::BASIC ->value ) {
14+ return $ path . 'BasicVigenereCipher ' ;
15+ } elseif ($ mode == VigenereMode::ALPHA_NUMERIC ->value ) {
16+ return $ path . 'AlnumVigenereCipher ' ;
17+ }
4418 }
4519
46- /**
47- * Set key
48- * We loop the key then concatenate it until it has the same length with the plain text
49- * Example:
50- * Plain text: vigenerecipher (14 characters)
51- * Key: abcd (4 characters)
52- * Repeated key: abcdabcdabcdab (14 characters)
53- *
54- * @param string key
55- * @return void
56- */
57- public function setKey ($ key )
20+ public static function encrypt (string $ data , string $ key , string $ mode = 'basic ' ): string
5821 {
59- $ plainTextLength = strlen ($ this ->plain_text );
60- $ keyLength = strlen ($ key );
61- $ messageLength = strlen ($ this ->plain_text );
22+ $ className = self ::getClassName ($ mode );
6223
63- $ repeatTimes = floor ($ messageLength / $ keyLength );
64- $ paddingKeyLength = $ messageLength - ($ keyLength * $ repeatTimes );
65-
66- $ repeatedKey = '' ;
24+ $ processName = ProcessType::ENCRYPT ->value ;
6725
68- for ($ i = 0 ; $ i < $ repeatTimes ; $ i ++) {
69- $ repeatedKey .= $ key ;
70- }
71-
72- $ paddedKey = $ repeatedKey . substr ($ key , 0 , $ paddingKeyLength );
73-
74- $ this ->key = $ paddedKey ;
75- }
26+ $ encrypt = new $ className ($ processName , $ data , $ key );
7627
77- /**
78- * Method to encrypt the plain text
79- *
80- * @return string
81- */
82- public function encrypt (): string
83- {
84- $ messageLength = strlen ($ this ->plain_text );
85- $ cipher = '' ;
86-
87- for ($ i = 0 ; $ i < $ messageLength ; $ i ++) {
88- $ messageCharPosition = strpos (self ::TABULA_RECTA , substr ($ this ->plain_text , $ i , 1 ));
89- $ keyCharPosition = strpos (self ::TABULA_RECTA , substr ($ this ->key , $ i , 1 ));
90-
91- $ shift = $ messageCharPosition + $ keyCharPosition ;
92- $ cipherCharPosition = $ shift % strlen (self ::TABULA_RECTA );
93- $ cipher .= substr (self ::TABULA_RECTA , $ cipherCharPosition , 1 );
94- }
95-
96- return $ cipher ;
28+ return $ encrypt ->getCipherText ();
9729 }
9830
99- /**
100- * Method to get plain text
101- *
102- * @return string
103- */
104- public function getPlainText (): string
31+ public static function decrypt (string $ data , string $ key , string $ mode = 'basic ' ): string
10532 {
106- return $ this ->plain_text ;
107- }
33+ $ className = self ::getClassName ($ mode );
10834
109- /**
110- * Method to get key
111- *
112- * @return string
113- */
114- public function getKey (): string
115- {
116- return $ this ->key ;
117- }
35+ $ processName = ProcessType::DECRYPT ->value ;
11836
119- /**
120- * Method to get cipher text
121- *
122- * @return string
123- */
124- public function getCipherText (): string
125- {
126- return $ this ->encrypt ();
37+ $ decrypt = new $ className ($ processName , $ data , $ key );
38+
39+ return $ decrypt ->getPlainText ();
12740 }
12841}
129- ?>
0 commit comments