-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarCipherContinuousLoop.py
More file actions
executable file
·146 lines (132 loc) · 5.42 KB
/
caesarCipherContinuousLoop.py
File metadata and controls
executable file
·146 lines (132 loc) · 5.42 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def askUser():
invalidMode= True
while(invalidMode):
print 'do you want to encrypt (type encrypt and press enter), decrypt (type decrypt and enter) or brute force (type force and enter)?'
mode=raw_input()
if(mode == "encrypt" or mode == "decrypt" or mode == "force"):
invalidMode= False
else:
print "sorry, invalid input - try again!"
return mode
def main():
currentMode = askUser()
print "you want to " + currentMode + " - awesome, let's get started!"
print "first type in the message you want to " + currentMode + " > "
message=raw_input().lower()
if(currentMode == "encrypt"):
currentKey = getKey()
encrypt(message, currentKey)
elif(currentMode == "decrypt"):
currentKey = getKey()
decrypt(message, currentKey)
elif(currentMode == "force"):
force(message)
def getKey():
##################################################
#Get encryption key from user
##################################################
invalidKey = True
cipherKey = 0
while(invalidKey):
print "please enter the key you want to use (type a number between 1 and 25 and press enter)"
cipherKey = raw_input()
#added call to method which checks
#if the number entered is a valid key, if it is
#then cast to an int and set invalidKey to be false
if(isValidKey(cipherKey)):
cipherKey = int(cipherKey)
invalidKey = False
#otherwise print invalid key and start loop again
else:
print("sorry, invalid key - try again!")
return cipherKey
###################################################
#Check if a value is an integer between 1 and 25
#returns true if it is a valid key, false otherwise
###################################################
def isValidKey(val):
#try to cast the value to an int, catch a Value error
#if it casts to an int without error, then check
#if it's between 1 an 25
#if it is, return true, else return false
try:
intVal=int(val)
if((intVal >= 1) and (intVal <= 25)):
return True
else:
return False
#return false if it's not a number
except ValueError:
print 'try entering a number'
return False
def force(msg):
##################################################
#brute force decrypt - i.e. try all possible keys
##################################################
print "attempting to force decrypt of " + msg + " now! - results are:"
##################################################
#outer loop - letters of alphabet
##################################################
for shiftValue in range(1,26):
print "For key = " + str(shiftValue) + " the decrypted message is: "
result = ""
##################################################
#inner loop - letters of message
##################################################
for letter in msg:
if(letter == ' '):
result += letter
else:
num = ord(letter)
num -= int(shiftValue)
if(num > ord('z')):
num -= ALPHABET_SIZE
if(num < ord('a')):
num += ALPHABET_SIZE
decryptedLetter = chr(num)
result += decryptedLetter
######################
# inner loop ends here
#######################
#back to outer loop
print result
print ""
#end of outer loop
def decrypt(msg, shiftValue):
print "decrypting " + msg + " now! - your decrypted message is:"
result = ""
for letter in msg:
if(letter == ' '):
result += letter
else:
num = ord(letter)
num -= shiftValue
if(num > ord('z')):
num -= ALPHABET_SIZE
if(num < ord('a')):
num += ALPHABET_SIZE
decryptedLetter = chr(num)
result += decryptedLetter
print result
def encrypt(msg, shiftValue):
print "encrypting " + msg + " now! - your encrypted message is:"
result = ""
for letter in msg:
if(letter == ' '):
result += letter
else:
num = ord(letter)
num += shiftValue
if(num > ord('z')):
num -= ALPHABET_SIZE
if(num < ord('a')):
num += ALPHABET_SIZE
encryptedLetter = chr(num)
result += encryptedLetter
print result
#get those who struggle to switch the value hard code, those who do well write a function to ask for shift value
#shiftValue=6
ALPHABET_SIZE=26
while True:
main()
print ""