Skip to content

Commit 64b8670

Browse files
authored
Merge pull request #1 from amculin/dev
Refactor, Support Alpha-Numeric mode
2 parents f136a9f + 421e44a commit 64b8670

File tree

7 files changed

+304
-113
lines changed

7 files changed

+304
-113
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2-
"name": "amculin/classic-encryption",
2+
"name": "amculin/vigenere-cipher",
33
"license": "MIT",
4-
"description": "Simple implementation of classic cryptography algorithm using PHP",
4+
"description": "Implementation of Vigenere Cipher algorithm in PHP",
5+
"keywords": [
6+
"php", "encryption", "decryption", "vigenere cipher", "classic cryptography", "cryptography",
7+
"encrypt", "decrypt"
8+
],
59
"authors": [
610
{
711
"name": "Fahmi Auliya Tsani",
@@ -11,7 +15,7 @@
1115
],
1216
"autoload": {
1317
"psr-4": {
14-
"amculin\\encryption\\": "source/"
18+
"amculin\\cryptography\\classic\\": "source/"
1519
}
1620
}
1721
}

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: 22 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,41 @@
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

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-
* 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

Comments
 (0)