forked from hacktoberfest17/programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvignere.py
More file actions
96 lines (87 loc) · 2.5 KB
/
Copy pathvignere.py
File metadata and controls
96 lines (87 loc) · 2.5 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
#!/usr/bin/python
# Uses Python2, need to edit print statements for python3 compatibility
# usage:
# python vigenere.py <-e,-d> <keyword> <message>
import sys
keyword = "A"
message = ["Hello","World"]
flag = 1
if(len(sys.argv) == 1):
print("What do you want to do ?\n1) Encrypt \n2) Decrypt")
i = input()
if(i<=0 or i>2):
print("Invalid option")
exit()
flag =i
print("Enter Keyword: ")
keyword = raw_input()
if(keyword.isalpha()==False):
print("Only alphabets allowed. No spaces.")
exit()
print("Enter message: ")
message = raw_input().split(' ')
for t in message:
if(t.isalpha()==False):
print("Only alphabets allowed in message.")
exit()
elif (len(sys.argv) >= 3):
trigger = sys.argv[1]
if(trigger == "-e"):
flag = 1
elif(trigger == "-d"):
flag = 2
else:
print("Invalid Flag.")
keyword = sys.argv[2]
message = sys.argv[3:]
if(keyword.isalpha()==False):
print("Usage: python vignere.py -e <KeyWord> <Message>.\nOnly alphabets allowed in Keyword.\n-e to encrypt, -d to decrypt")
exit()
for t in message:
if(t.isalpha()==False):
print("Usage: python vignere.py <KeyWord> <Message>.\nOnly alphabets allowed in message.\n-e to encrypt, -d to decrypt")
exit()
else:
print("Usage: python vignere.py <KeyWord> <Message>.\nOnly alphabets allowed in both keyword and message.\nNo spaces in the keyword allowed.\n-e to encrypt, -d to decrypt")
j = 0
def encrypt(key, mess):
global j
key = key.lower()
res = ""
mess = mess.lower()
for i in range(0,len(mess)):
shiftamt = ord(key[j]) - ord('a')
x = ord(mess[i]) + shiftamt
while (x > ord('z')):
x -= (ord('z')-ord('a'))
while (x < ord('a')):
x += (ord('z')-ord('a'))
res = res + chr(x)
j = (j+1)%(len(key))
return res
def decrypt(key, mess):
global j
key = key.lower()
res = ""
mess = mess.lower()
for i in range(0,len(mess)):
shiftamt = ord(key[j]) - ord('a')
x = ord(mess[i]) - shiftamt
while (x > ord('z')):
x -= (ord('z')-ord('a'))
while (x < ord('a')):
x += (ord('z')-ord('a'))
res = res + chr(x)
j = (j+1)%(len(key))
return res
# Instructions to use : Can be used both, interactively as well as via command line arguments
# Via command line parameters:
# Use for encryption : python vignere.py -e lemon attack at dawn
# Use for decryption : python vignere.py -d lemon lxgopv eg rnir
# Interactively just follow the prompts
if(flag == 1):
for i in range(0,len(message)):
print encrypt(keyword, message[i]),
elif(flag ==2):
for i in range(0,len(message)):
print decrypt(keyword, message[i]),