-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereCipher2.7-1.py
More file actions
125 lines (114 loc) · 4.46 KB
/
VigenereCipher2.7-1.py
File metadata and controls
125 lines (114 loc) · 4.46 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
###################################################
#Does user want to encrypt or decrypt?
###################################################
def askUserVig():
invalidMode= True
while(invalidMode):
print 'do you want to encrypt (type encrypt and press enter) or decrypt (type decrypt and enter)?'
mode=raw_input()
if(mode == "encrypt" or mode == "decrypt"):
invalidMode= False
else:
print "sorry, invalid input - try again!"
return mode
###################################################
#Get a keyword for Vigenere cipher
#returns keyword if valid, false otherwise
###################################################
def getKeyword():
invalidKeyword = True
cipherKeyword = ''
while(invalidKeyword):
print "please enter the keyword you want to use (type in a word and press enter)"
cipherKeyword= raw_input()
#added call to method which checks
#if the number entered is a valid keyword, if it is
#set invalidKeyword to be false
if(isValidKeyword(cipherKeyword)):
invalidKeyword = False
#otherwise print invalid key and start loop again
else:
print("sorry, invalid keyword - try again! (Keyword must only contain letters a-z)")
return cipherKeyword
###################################################
#Check if a value is a word made up of letters a-z
#returns true if it is a valid keyword, false otherwise
###################################################
def isValidKeyword(val):
isValid = True
lenKeyword = len(val)
#check if word is made of letters a - z
#if it is, return true, else return false
for i in range(lenKeyword):
if (ord(val[i]) < (ord('a'))) or ( ord('z') < ord(val[i])):
return False
return isValid
##############################################
# encrypt or decrypt using vigenere cipher
###############################################
def vigenere(message, keyword, direction):
keywordLength = len(keyword)
msgLength = len(message)
result = ""
if direction == 'encrypt':
for i in range(0,msgLength):
nextLetter = encrypt((message[i]), ord(keyword[(i%keywordLength)])-97)
result += nextLetter
elif direction == "decrypt":
for i in range(0,msgLength ):
nextLetter = decrypt(message[i], ord(keyword[i%keywordLength]) - 97)
result += nextLetter
print result
##############################################
# decrypt message - as used in Caesar Cipher
############################################
def decrypt(msg, shiftValue):
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
return result
##############################################
# decrypt message - as used in Caesar Cipher
############################################
def encrypt(msg, shiftValue):
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
return result
############################################
# main program
############################################
def main():
print "Welcome to the ' + EVIL_ORGANISATION + ' Vigenere cipher program."
#ask user if they want to encrypt or decrypt
direction = askUserVig()
print "first type in the message you want to " + direction + " in lower case letters > "
#get message
message=raw_input()
#get keyword
keyword = getKeyword()
#apply cipher
vigenere(message, keyword, direction)
ALPHABET_SIZE=26
EVIL_ORGANISATION = 'E.V.I.L.'
main()