Welcome to the Caesar Cipher project! This repository contains a simple implementation of the Caesar Cipher algorithm in Python. The Caesar Cipher is one of the earliest and simplest methods of encryption, named after Julius Caesar, who reportedly used it to communicate securely with his officials.
The Caesar Cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of places down or up the alphabet. This project includes two main functions: encrypt and decrypt, as well as a main function to interact with the user.
The encrypt function takes two parameters: the text to be encrypted and the shift value, which determines how many places each character in the text is shifted. Here’s how it works:
- Initialization: An empty string
resultis initialized to store the encrypted text. - Iteration: The function iterates over each character in the input
text. - Character Check: For each character, it checks if the character is an alphabet letter using
char.isalpha(). - Case Check: It determines whether the character is uppercase or lowercase to handle them separately.
- Shift Calculation: It calculates the new character by shifting the original character by the
shiftvalue. The calculation involves:- Getting the ASCII value of the character using
ord(). - Applying the shift and adjusting for wrap-around within the alphabet using modulo operation.
- Converting the shifted ASCII value back to a character using
chr().
- Getting the ASCII value of the character using
- Appending: The shifted character is appended to the
resultstring. Non-alphabet characters are appended without changes. - Return: The final encrypted string is returned.
The decrypt function simply reverses the encryption process. It calls the encrypt function with the negative of the shift value, effectively reversing the shift applied during encryption.
The main function is the entry point of the program. It interacts with the user to get the message and shift value, then displays both the encrypted and decrypted messages.
- Input: Prompts the user to enter the message and shift value.
- Encryption: Calls the
encryptfunction with the user input to get the encrypted message. - Decryption: Calls the
decryptfunction with the encrypted message to verify the decryption process. - Output: Prints the encrypted and decrypted messages.
To use this program, run the caesar_cipher.py script and follow the on-screen instructions to enter your message and shift value. The program will display the encrypted message and then decrypt it back to the original message.
def main():
message = input("Enter the message: ")
shift = int(input("Enter the shift value: "))
encrypted_message = encrypt(message, shift)
decrypted_message = decrypt(encrypted_message, shift)
print(f"\nEncrypted message: {encrypted_message}")
print(f"Decrypted message: {decrypted_message}")
if _name_ == "_main_":
main()'''pythonEnter the message: Hello World!
Enter the shift value: 3
Encrypted message: Khoor Zruog!
Decrypted message: Hello World!
Feel free to fork this repository and submit pull requests. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License. See the LICENSE file for details.
Happy coding! 🚀