Skip to content

Commit

Permalink
Merge pull request #123 from aryanshb/master
Browse files Browse the repository at this point in the history
Added Vignere Cipher
  • Loading branch information
darwinz authored Oct 7, 2023
2 parents 618fe7f + 831d93b commit a65714c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
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);
}
}

0 comments on commit a65714c

Please sign in to comment.