├── LICENSE ├── README.md └── src └── CaesarCipher.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Ramazan Çetinkaya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Caesar Cipher Library 2 | 3 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ramazancetinkaya/caesar-cipher/blob/master/LICENSE) 4 | 5 | A PHP library for encrypting and decrypting text using the Caesar Cipher algorithm. 6 | 7 | ## Caesar Cipher 8 | 9 | In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code, or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence. 10 | 11 | ## Usage 12 | 13 | ```php 14 | // Create a CaesarCipher instance with a shift of 3 15 | $caesarCipher = new CaesarCipher(3); 16 | 17 | $plaintext = "Hello, World!"; 18 | $encryptedText = $caesarCipher->encrypt($plaintext); 19 | $decryptedText = $caesarCipher->decrypt($encryptedText); 20 | 21 | echo "Original Text: $plaintext\n"; 22 | echo "Encrypted Text: $encryptedText\n"; 23 | echo "Decrypted Text: $decryptedText\n"; 24 | ``` 25 | 26 | ## License 27 | 28 | This project is licensed under the MIT License - see the LICENSE file for details. 29 | -------------------------------------------------------------------------------- /src/CaesarCipher.php: -------------------------------------------------------------------------------- 1 | 25) { 35 | throw new InvalidArgumentException("Shift value must be an integer between 1 and 25."); 36 | } 37 | 38 | $this->shift = $shift; 39 | } 40 | 41 | /** 42 | * Encrypts a given plaintext using the Caesar Cipher. 43 | * 44 | * @param string $plaintext The text to be encrypted. 45 | * @return string The encrypted text. 46 | */ 47 | public function encrypt($plaintext) 48 | { 49 | // Validate input 50 | if (!is_string($plaintext)) { 51 | throw new InvalidArgumentException("Input must be a string."); 52 | } 53 | 54 | $encryptedText = ''; 55 | 56 | // Iterate through each character in the plaintext 57 | for ($i = 0; $i < strlen($plaintext); $i++) { 58 | $char = $plaintext[$i]; 59 | 60 | // Check if the character is alphabetic 61 | if (ctype_alpha($char)) { 62 | $isUppercase = ctype_upper($char); 63 | $char = strtolower($char); 64 | 65 | // Apply the Caesar Cipher shift 66 | $char = chr(((ord($char) - ord('a') + $this->shift) % 26) + ord('a')); 67 | 68 | // Convert back to uppercase if needed 69 | if ($isUppercase) { 70 | $char = strtoupper($char); 71 | } 72 | } 73 | 74 | $encryptedText .= $char; 75 | } 76 | 77 | return $encryptedText; 78 | } 79 | 80 | /** 81 | * Decrypts a given ciphertext using the Caesar Cipher. 82 | * 83 | * @param string $ciphertext The text to be decrypted. 84 | * @return string The decrypted text. 85 | */ 86 | public function decrypt($ciphertext) 87 | { 88 | // Validate input 89 | if (!is_string($ciphertext)) { 90 | throw new InvalidArgumentException("Input must be a string."); 91 | } 92 | 93 | // To decrypt, we use a negative shift value 94 | $this->shift = -$this->shift; 95 | 96 | // Use the encrypt method for decryption 97 | $decryptedText = $this->encrypt($ciphertext); 98 | 99 | // Restore the original shift value 100 | $this->shift = abs($this->shift); 101 | 102 | return $decryptedText; 103 | } 104 | } 105 | 106 | ?> 107 | --------------------------------------------------------------------------------