-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplemet_Caesar_Cipher.py
More file actions
36 lines (32 loc) · 1.07 KB
/
Implemet_Caesar_Cipher.py
File metadata and controls
36 lines (32 loc) · 1.07 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
def encrypt_text(plaintext, n):
ans = ""
for i in range(len(plaintext)):
ch = plaintext[i]
if ch == " ":
ans += " "
elif (ch.isupper()):
ans += chr((ord(ch) + n - 65) % 26 + 65)
else:
ans += chr((ord(ch) + n - 97) % 26 + 97)
return ans
plaintext = "Prodigy Info TechE"
n = 3
print("Plain Text is : " + plaintext)
print("Shift pattern is : " + str(n))
print("Cipher Text is : " + encrypt_text(plaintext, n))
def decrypt():
encrypted_message = input("Enter the message i.e to be decrypted: ").strip()
letters = "abcdefghijklmnopqrstuvwxyz"
k = int(input("Enter the key to decrypt: "))
decrypted_message = ""
for ch in encrypted_message:
if ch in letters:
position = letters.find(ch)
new_pos = (position - k) % 26
new_char = letters[new_pos]
decrypted_message += new_char
else:
decrypted_message += ch
print("Your decrypted message is:\n")
print(decrypted_message)
decrypt()