-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from aryanshb/master
Added Vignere Cipher
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |