forked from prateekiiest/Code-Sleep-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
executable file
·92 lines (84 loc) · 2.52 KB
/
caesar.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
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
import sys
from string import punctuation
def remove_punctuation(content):
content = content.translate(None, punctuation); #removing !"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
content = ''.join(content.split()) #removing tabs, spaces, newlines
return content.lower()
def take_input():
try:
k = int(raw_input("Enter key: "))
return k
except Exception as e:
print "Error : ", e
sys.exit(0)
def file_input():
try:
fp = open(raw_input("Enter file: "), "r")
content = fp.read()
fp.close()
return content
except Exception as e:
print "Error : ", e
sys.exit(1)
def display_format(message):
''' Print the final encoded message in blocks of five letters and ten blocks per line.
The last line may be shorter than five blocks, and the last block may be shorter
than five letters. '''
output = []
enc = ""
while message:
output.append(message[:5])
message = message[5:]
for index, block in enumerate(output):
if((index + 1) % 10 == 0):
enc = enc + block + "\n"
else:
enc = enc + block + " "
return enc
def encrypt(content, k):
encrypted_message = ""
print "Discarding the punctuation..."
content = remove_punctuation(content)
print "Encrypting the text..."
for char in content:
encrypted_message += chr((ord(char) - ord('a') + k)%26 + ord('a')) #ord func gives ascii value #chr func gives character
encrypted_message = display_format(encrypted_message)
return encrypted_message
def decrypt(content):
content = remove_punctuation(content)
print "Decryting the text with each key and writing to the file \"decrypt.output\"..."
try:
fp = open("decrypt.output", "w");
for k in range(26):
plain_text = ""
for char in content:
plain_text += chr((ord(char) - ord('a') - (k+1))%26 + ord('a')) #ord func gives ascii value #chr func gives character
plain_text = display_format(plain_text)
fp.write("Key " + str(k+1) + " :\n")
fp.write(plain_text + "\n\n")
fp.close()
except Exception as e:
print "Error : ", e
def main():
try:
case = int(raw_input("1 to encrypt plain_text from file\n2 to decrypt ciphertext from file\nEnter option : "))
if(case == 1):
k = take_input()
file_content = file_input()
encrypted_message = encrypt(file_content, k)
print "Encrypted Message..."
print encrypted_message
sys.exit(0)
if(case == 2):
encrypted_message = file_input()
decrypt(encrypted_message)
sys.exit(0)
else:
print "Wrong option"
sys.exit(0)
except KeyboardInterrupt:
print "\nClosing the program..."
sys.exit(0)
if __name__ == '__main__':
main()