Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"name": "amculin/classic-encryption",
"name": "amculin/vigenere-cipher",
"license": "MIT",
"description": "Simple implementation of classic cryptography algorithm using PHP",
"description": "Implementation of Vigenere Cipher algorithm in PHP",
"keywords": [
"php", "encryption", "decryption", "vigenere cipher", "classic cryptography", "cryptography",
"encrypt", "decrypt"
],
"authors": [
{
"name": "Fahmi Auliya Tsani",
Expand All @@ -11,7 +15,7 @@
],
"autoload": {
"psr-4": {
"amculin\\encryption\\": "source/"
"amculin\\cryptography\\classic\\": "source/"
}
}
}
20 changes: 20 additions & 0 deletions source/AlnumVigenereCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace amculin\cryptography\classic;

/**
* This file is the main class for alpha-numric mode vigenere cipher algortithm
*
* @author Fahmi Auliya Tsani <amixcustomlinux@gmail.com>
* @version 0.1
*/

class AlnumVigenereCipher extends VigenereCipherBlueprint
{
/**
* Default list of acceptable character to be used in vigenere cipher algorithm
* This list cointains alpha-numeric characters including the capitalized
*
* @var string
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
}
17 changes: 17 additions & 0 deletions source/BasicVigenereCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace amculin\cryptography\classic;

/**
* This file is the main class for basic vigenere cipher algortithm
*
* @author Fahmi Auliya Tsani <amixcustomlinux@gmail.com>
* @version 0.2
*/

class BasicVigenereCipher extends VigenereCipherBlueprint
{
/**
* @inheritdoc
*/
public $tabulaRecta = 'abcdefghijklmnopqrstuvwxyz';
}
132 changes: 22 additions & 110 deletions source/VigenereCipher.php
Original file line number Diff line number Diff line change
@@ -1,129 +1,41 @@
<?php
namespace amculin\encryption;
namespace amculin\cryptography\classic;

/**
* This file is the main class for vigenere cipher encryption algortithm
*
* @author Fahmi Auliya Tsani <amixcustomlinux@gmail.com>
* @version 0.1
*/
use amculin\cryptography\classic\enums\ProcessType;
use amculin\cryptography\classic\enums\VigenereMode;

class VigenereCipher
{
/**
* Default list of acceptable character to be encrypted using vigenere
* By default, it just an alphabetical list.
*
* @var string
*/
const TABULA_RECTA = 'abcdefghijklmnopqrstuvwxyz';

/**
* The plain text/message to be encrypted
*
* @var string
*/
public $plain_text;

/**
* The key used to encrypt plain text/message
*
* @var string
*/
public $key;

/**
* Set the plain text/message to be encrypted
*
* @param string message
* @return void
*/
public function setPlainText($message)
public static function getClassName(string $mode): string
{
$this->plain_text = $message;
$path = 'amculin\cryptography\classic\\';

if ($mode == VigenereMode::BASIC->value) {
return $path . 'BasicVigenereCipher';
} elseif ($mode == VigenereMode::ALPHA_NUMERIC->value) {
return $path . 'AlnumVigenereCipher';
}
}

/**
* Set key
* We loop the key then concatenate it until it has the same length with the plain text
* Example:
* Plain text: vigenerecipher (14 characters)
* Key: abcd (4 characters)
* Repeated key: abcdabcdabcdab (14 characters)
*
* @param string key
* @return void
*/
public function setKey($key)
public static function encrypt(string $data, string $key, string $mode = 'basic'): string
{
$plainTextLength = strlen($this->plain_text);
$keyLength = strlen($key);
$messageLength = strlen($this->plain_text);
$className = self::getClassName($mode);

$repeatTimes = floor($messageLength / $keyLength);
$paddingKeyLength = $messageLength - ($keyLength * $repeatTimes);

$repeatedKey = '';
$processName = ProcessType::ENCRYPT->value;

for ($i = 0; $i < $repeatTimes; $i++) {
$repeatedKey .= $key;
}

$paddedKey = $repeatedKey . substr($key, 0, $paddingKeyLength);

$this->key = $paddedKey;
}
$encrypt = new $className($processName, $data, $key);

/**
* Method to encrypt the plain text
*
* @return string
*/
public function encrypt(): string
{
$messageLength = strlen($this->plain_text);
$cipher = '';

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

$shift = $messageCharPosition + $keyCharPosition;
$cipherCharPosition = $shift % strlen(self::TABULA_RECTA);
$cipher .= substr(self::TABULA_RECTA, $cipherCharPosition, 1);
}

return $cipher;
return $encrypt->getCipherText();
}

/**
* Method to get plain text
*
* @return string
*/
public function getPlainText(): string
public static function decrypt(string $data, string $key, string $mode = 'basic'): string
{
return $this->plain_text;
}
$className = self::getClassName($mode);

/**
* Method to get key
*
* @return string
*/
public function getKey(): string
{
return $this->key;
}
$processName = ProcessType::DECRYPT->value;

/**
* Method to get cipher text
*
* @return string
*/
public function getCipherText(): string
{
return $this->encrypt();
$decrypt = new $className($processName, $data, $key);

return $decrypt->getPlainText();
}
}
?>
Loading
Loading