-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.py
executable file
·52 lines (45 loc) · 1.12 KB
/
vigenere.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
#!/usr/bin/python
import sys
from collections import defaultdict
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CLEF = "IFOSUP"
def rotation(s):
n = s[1:]
n += s[0]
return n
def setGrille():
grille = defaultdict(lambda: defaultdict(dict))
al = ALPHABET
for x in ALPHABET:
num = 0
for y in ALPHABET:
grille[y][x] = al[num]
num += 1
al = rotation(al)
return grille
def chiffrer(message):
grille = setGrille()
chiffre = ''
kpos = 0
for clair in message.replace(' ',''):
if kpos >= len(CLEF):
kpos = 0
chiffre += grille[CLEF[kpos]][clair]
kpos += 1
return chiffre
def dechiffrer(message):
grille = setGrille()
dechiffre = ''
kpos = 0
for pasclair in message.replace(' ',''):
if kpos >= len(CLEF):
kpos = 0
for k,value in grille[CLEF[kpos]].iteritems():
if value == pasclair:
dechiffre += k
kpos += 1
return dechiffre
if sys.argv[1] == '-c':
print chiffrer(sys.argv[2])
elif sys.argv[1] == '-d':
print dechiffrer(sys.argv[2])