Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Vignere Cipher #123

Merged
merged 4 commits into from
Oct 7, 2023
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
63 changes: 63 additions & 0 deletions Ciphers/VignereCipher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Encrypts a plaintext using the Vigenère cipher.
* (https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher)
*
* @param string $plaintext The plaintext to be encrypted.
* @param string $key The encryption key.
* @return string The encrypted text.
*/
function vigenere_encrypt($plaintext, $key): string
{
// Convert the input to uppercase for consistency
$plaintext = strtoupper($plaintext);
$key = strtoupper($key);
$keyLength = strlen($key);
$encryptedText = "";
for ($i = 0; $i < strlen($plaintext); $i++) {
$char = $plaintext[$i];
if (ctype_alpha($char)) {
// Calculate the shift based on the key
$shift = ord($key[$i % $keyLength]) - ord('A');
// Apply the Vigenère encryption formula
$encryptedChar = chr(((ord($char) - ord('A') + $shift) % 26) + ord('A'));
// Append the encrypted character to the result
$encryptedText .= $encryptedChar;
} else {
// If the character is not alphabetic, leave it unchanged
$encryptedText .= $char;
}
}
return $encryptedText;
}

/**
* Decrypts a ciphertext using the Vigenère cipher.
*
* @param string $ciphertext The ciphertext to be decrypted.
* @param string $key The decryption key.
* @return string The decrypted text.
*/
function vigenere_decrypt($ciphertext, $key): string
{
$ciphertext = strtoupper($ciphertext);
$key = strtoupper($key);
$keyLength = strlen($key);
$decryptedText = "";
for ($i = 0; $i < strlen($ciphertext); $i++) {
$char = $ciphertext[$i];
if (ctype_alpha($char)) {
// Calculate the shift based on the key
$shift = ord($key[$i % $keyLength]) - ord('A');
// Apply the Vigenère decryption formula
$decryptedChar = chr(((ord($char) - ord('A') - $shift + 26) % 26) + ord('A'));
// Append the decrypted character to the result
$decryptedText .= $decryptedChar;
} else {
// If the character is not alphabetic, leave it unchanged
$decryptedText .= $char;
}
}
return $decryptedText;
}
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [Caesarcipher](./Ciphers/CaesarCipher.php)
* [Monoalphabeticcipher](./Ciphers/MonoAlphabeticCipher.php)
* [Morsecode](./Ciphers/MorseCode.php)
* [Vignerecipher](./Ciphers/VignereCipher.php)
* [Xorcipher](./Ciphers/XORCipher.php)

## Conversions
Expand Down Expand Up @@ -89,6 +90,7 @@
* [Cipherstest](./tests/Ciphers/CiphersTest.php)
* [Monoalphabeticciphertest](./tests/Ciphers/MonoAlphabeticCipherTest.php)
* [Morsecodetest](./tests/Ciphers/MorseCodeTest.php)
* [Vignereciphertest](./tests/Ciphers/VignereCipherTest.php)
* Conversions
* [Conversionstest](./tests/Conversions/ConversionsTest.php)
* Datastructures
Expand Down
19 changes: 19 additions & 0 deletions tests/Ciphers/VignereCipherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use PHPUnit\Framework\TestCase;

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../Ciphers/VignereCipher.php';

class VignereCipher extends TestCase
{
public function testVignereCipher()
{
$plaintext = "HELLO";
$key = "KEY";
$encryptedText = vigenere_encrypt($plaintext, $key);
$decryptedText = vigenere_decrypt($encryptedText, $key);

$this->assertEquals($plaintext, $decryptedText);
}
}
Loading