-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar cipher.py
More file actions
82 lines (67 loc) · 3.33 KB
/
Copy pathCaesar cipher.py
File metadata and controls
82 lines (67 loc) · 3.33 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Simple Caesar cipher functions with validations.
# This file provides:
# - caesar(text, shift, encrypt=True): core logic to shift letters.
# - encrypt(text, shift): convenience wrapper for encryption.
# - decrypt(text, shift): convenience wrapper for decryption.
# Example usage at the bottom demonstrates decryption of a sample string.
def caesar(text,shift,encrypt=True):
"""Perform Caesar cipher encryption or decryption on the input text.
Parameters:
- text (str): The input text to be processed.
- shift (int): The number of positions each letter should be shifted.
- encrypt (bool): If True, encrypt the text; if False, decrypt the text.
Returns:
- str: The encrypted or decrypted text.
"""
# Validate argument types early so the rest of the function can assume valid inputs.
# We expect 'shift' to be an integer between 1 and 25 inclusive.
if not isinstance(shift,int):
return "shift must be an integer Value"
if shift<1 or shift>25:
return "shift must be an integer between 1 and 25"
# Define a lowercase alphabet for creating the shift map.
# We will also handle uppercase by using .upper() when creating the translation table.
alphabet="abcdefghijklmnopqrstuvwxyz"
# If we're decrypting rather than encrypting, reverse the direction of the shift.
# For example, a shift of 3 for encryption becomes -3 for decryption.
if not encrypt:
shift=-shift
# Build the shifted version of the alphabet.
# Example: with shift=3 -> 'defghijklmnopqrstuvwxyzabc'
shifted_alphabet=alphabet[shift:] + alphabet[:shift]
# Create a translation table for str.translate:
# - We provide lowercase alphabet and uppercase alphabet as the 'from' set.
# - We provide shifted lowercase and shifted uppercase as the 'to' set.
# str.maketrans maps each character positionally from 'from' to 'to'.
translation_table=str.maketrans(alphabet+alphabet.upper(),
shifted_alphabet+shifted_alphabet.upper())
# Apply the translation to the input text and return the processed string.
# Non-alphabet characters (digits, punctuation, whitespace) are unchanged by translate.
encrypted_text=text.translate(translation_table)
return encrypted_text
# Helper functions to make the API clearer.
# They simply call the core 'caesar' function with the appropriate flag.
def encrypt(text,shift):
"""Encrypt the input text using Caesar cipher with the given shift.
Parameters:
- text (str): The input text to be encrypted.
- shift (int): The number of positions each letter should be shifted.
Returns:
- str: The encrypted text.
"""
# Default is encrypt=True, so just call caesar.
return caesar(text,shift)
def decrypt(text,shift):
"""Decrypt the input text which was encrypted with Caesar cipher.
Parameters:
- text (str): The encrypted text to be decrypted.
- shift (int): The number of positions each letter was shifted during encryption.
Returns:
- str: The decrypted text.
"""
# Explicitly pass encrypt=False to reverse the shift.
return caesar(text,shift,encrypt=False)
# Example: decrypt a sample message encrypted with a shift of 13 (ROT13).
encrypted_text="Pbhentr vf sbhaq va hayvxryl cynprf."
decrypted_text=decrypt(encrypted_text,13)
print(decrypted_text)