-
Notifications
You must be signed in to change notification settings - Fork 4
/
CaesarCipher.py
108 lines (92 loc) · 4.11 KB
/
CaesarCipher.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
Edward Aryee
Python Language Projects
Caesar cipher
Implement a Caesar cipher, both encoding and decoding. The key is an integer
from 1 to 25. This cipher rotates the letters of the alphabet (A to Z). The
encoding replaces each letter with the 1st to 25th next letter in the alphabet
(wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to
"BC". This simple "monoalphabetic substitution cipher" provides almost no
security, because an attacker who has the encoded message can either use
frequency analysis to guess the key, or just try all 25 keys.
"""
from math import *
from string import *
from array import *
alphabet = {0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g',7:'h',8:'i',9:'j',10:'k',11:'l',12:'m',
13:'n',14:'o',15:'p',16:'q',17:'r',18:'s',19:'t',20:'u',21:'v',22:'w',23:'x',24:'y',25:'z'}
specChar = {0:"~",1:".",2:"?",3:";",4:":",5:"!",6:"@",7:"$",8:"%",9:"^",10:"&",11:"(",
12:")",13:"+",14:"-",15:"*",16:"/",17:"<",18:">",19:"`",20:"#",21:"\"",22:"'",23:","}
def isSpecialChar(element):
for char in specChar:
if(element == specChar[char]):
return True
return False
def isCharacter(element):
for letter in alphabet:
if(element.lower() == alphabet[letter]):
return True
return False
def isNumber(element):
for i in range(0,10):
if(int(element) == i):
return True
return False
def encrypt(message, encryptNum):
encryptedMessage = "";
for mElement in message:
if(mElement == " "):
encryptedMessage = encryptedMessage + " "
elif(mElement == "."):
encryptedMessage = encryptedMessage + "."
elif(isSpecialChar(mElement)):
for aCharacter in specChar:
if(mElement == specChar[aCharacter]):
eChar = (aCharacter + encryptNum)%24
encryptedMessage = encryptedMessage + specChar[eChar]
elif(isCharacter(mElement)):
for aLetter in alphabet:
if(mElement.lower() == alphabet[aLetter]):
eLetter = (aLetter + encryptNum)%26
encryptedMessage = encryptedMessage + alphabet[eLetter]
elif(isNumber(mElement)):
eNumber = (int(mElement) + encryptNum)%10
encryptedMessage = encryptedMessage + str(eNumber)
else:
encryptedMessage = encryptedMessage + mElement
print("\n%s \nDecryption Number: %d\n" %(encryptedMessage, encryptNum))
return encryptedMessage
def decrypt(message, decryptNum):
decryptedMessage = "";
for mElement in message:
if(mElement == " "):
decryptedMessage = decryptedMessage + " "
elif(mElement == "."):
encryptedMessage = encryptedMessage + "."
elif(isSpecialChar(mElement)):
for aCharacter in specChar:
if(mElement == specChar[aCharacter]):
deChar = (aCharacter - decryptNum)%24
decryptedMessage = decryptedMessage + specChar[deChar]
elif(isCharacter(mElement)):
for aLetter in alphabet:
if(mElement.lower() == alphabet[aLetter]):
deLetter = ((aLetter - decryptNum)%26)
decryptedMessage = decryptedMessage + alphabet[deLetter]
elif(isNumber(mElement)):
deNumber = (int(mElement) - decryptNum)%10
decryptedMessage = decryptedMessage + str(deNumber)
else:
decryptedMessage = decryptedMessage + mElement
print("Decrtypted Message: %s\n" %(decryptedMessage))
return decryptedMessage
if __name__ == '__main__':
deORen = input("Would you like to encrypt a message or decrypt a message? (e or d): ")
if(deORen == "e"):
message = input("Please enter message to encrypt: ")
encryptNum = int(input("Please enter the Caesar Cipher encryption number: "))
encrypt(message, encryptNum)
elif(deORen == "d"):
message = input("Please enter message to decrypt: ")
decryptNum = int(input("Please enter the Caesar Cipher encryption number: "))
decrypt(message,decryptNum)