-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEnigmaCrypt.py
82 lines (59 loc) · 2.71 KB
/
EnigmaCrypt.py
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
def caesar_encoder(plain_text, shift):
encoded_text = ""
for letter in plain_text:
if not letter.isalpha():
# Non-alphabetic characters are included as-is in the encoded text
encoded_text += letter
continue
# Determine the position of the letter in the alphabet (0-25)
position = ord(letter.lower()) - ord('a')
# Shift the position by the specified amount
new_position = (position + shift) % 26
# Convert the new position back to a letter
encoded_letter = chr(new_position + ord('a'))
# Use the original case of the letter
if letter.isupper():
encoded_letter = encoded_letter.upper()
encoded_text += encoded_letter
return encoded_text
def caesar_decoder(cipher_text, shift):
decoded_text = ""
for letter in cipher_text:
if not letter.isalpha():
# Non-alphabetic characters are included as-is in the decoded text
decoded_text += letter
continue
# Determine the position of the letter in the alphabet (0-25)
position = ord(letter.lower()) - ord('a')
# Shift the position by the specified amount
if shift >= 0:
new_position = (position - shift) % 26
else:
new_position = (position - shift) % 26
# Convert the new position back to a letter
decoded_letter = chr(new_position + ord('a'))
# Use the original case of the letter
if letter.isupper():
decoded_letter = decoded_letter.upper()
decoded_text += decoded_letter
return decoded_text
# Ask the user whether to encode plain text or decode a Caesar cipher
choice = input("Enter 'e' to encode plain text or 'd' to decode a Caesar cipher: ")
if choice == 'e':
# Ask the user for a message to encode
plain_text = input("Enter a message to encode: ")
# Ask the user for a shift value
shift = int(input("Enter the number of positions to shift the letters: "))
# Encode the message using the Caesar Cipher
cipher_text = caesar_encoder(plain_text, shift)
print("Encoded message:", cipher_text)
elif choice == 'd':
# Ask the user for a message to decode
cipher_text = input("Enter a message to decode: ")
# Ask the user for a shift value
shift = int(input("Enter the number of positions the letters were shifted: "))
# Decode the message using the Caesar Cipher
decoded_text = caesar_decoder(cipher_text, shift)
print("Decoded message:", decoded_text)
else:
print("Invalid choice. Please enter 'e' to encode plain text or 'd' to decode a Caesar cipher.")