-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathAtbashCipher.php
39 lines (37 loc) · 1.21 KB
/
AtbashCipher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
* Encrypt a message using the Atbash Cipher.
* The Atbash Cipher is a simple substitution cipher where each letter in the plaintext is
* replaced with its corresponding letter from the end of the alphabet (reverse alphabet).
* Non-alphabet characters are not modified.
*
* @param string $plainText The plaintext to encrypt.
* @return string The encrypted message.
*/
function atbash_encrypt($plainText)
{
$result = '';
$plainText = strtoupper($plainText);
for ($i = 0; $i < strlen($plainText); $i++) {
$char = $plainText[$i];
if (ctype_alpha($char)) {
$offset = ord('Z') - ord($char);
$encryptedChar = chr(ord('A') + $offset);
} else {
$encryptedChar = $char; // Non-alphabet characters remain unchanged
}
$result .= $encryptedChar;
}
return $result;
}
/**
* Decrypt a message encrypted using the Atbash Cipher.
* Since the Atbash Cipher is its own inverse, decryption is the same as encryption.
*
* @param string $cipherText The ciphertext to decrypt.
* @return string The decrypted message.
*/
function atbash_decrypt($cipherText)
{
return atbash_encrypt($cipherText); // Decryption is the same as encryption
}