-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar_cipher.py
More file actions
70 lines (61 loc) · 1.9 KB
/
Caesar_cipher.py
File metadata and controls
70 lines (61 loc) · 1.9 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
'''Caesar Cipher
Implement a Caesar cipher encryption and decryption program.
'''
text = input("Enter a text : ")
key=int(input("Enter a shift :"))
print(" Enter 1 to convert plain-Text to encrypted-text(Cipher Text). \n "
"Enter 2 to convert Cipher Text to decrypted-text(plain-Text).")
x=int(input("Enter your selection : "))
match x:
case 1:
print("Plain Text message to Cipher Text : ")
result = ''
for char in text:
if char.isalpha():
shift = 65 if char.isupper() else 97
result+= chr((ord(char) - shift + key ) %26 + shift)
else:
result+=char
print(result)
case 2:
print("Cipher Text message to Plain Text : ")
result = ''
for char in text:
if char.isalpha():
shift = 65 if char.isupper() else 97
result += chr((ord(char) - shift - key) % 26 + shift )
else:
result += char
print(result)
case _:
print("Invalid selection please enter right value !!!")
""" To convert plain-text to caesar text """
# def caesar(word ,key ):
# result =''
# for char in word:
# if char.isalpha():
# shift = 65 if char.isupper() else 97
# result+= chr((ord(char) - shift + key ) %26 + shift )
# else:
# result+=char
#
# return result
# word=input("Enter a word :")
# key=int(input("Enter a shift :"))
#
# print(caesar(word, key))
""" To convert cipher text to caesar text """
# def caesar(word ,key ):
# result =''
# for char in word:
# if char.isalpha():
# shift = 65 if char.isupper() else 97
# result += chr((ord(char) - shift - key) % 26 + shift )
# else:
# result+=char
#
# return result
# word=input("Enter a word :")
# key=int(input("Enter a shift :"))
#
# print(caesar(word, key))