mport sys
#usage of this program
# sytanx: vigenere --x file key
# pl.: vigenere --df szoveg.txt awordhere
#--e : cipher given txt with vigenere square
#--d: decipher given txt with vigenere square
#--ef: cipher txt and save it to a file
#--df: decipher txt and save it to a file
#the keyword can not include numbers, as well as the given text.
#my suggestion is to write it with letters
help="Use with --e,--d,--ef,--df options! [vigenere.exe --e/--d szoveg.txt awordhere]"
dfname='vig_decoded.txt'
efname='vig_ciphered.txt'
if len(sys.argv)<2:
print help
sys.exit()
if sys.argv[1] != "--e" and sys.argv[1] != "--d" and sys.argv[1] != "--ef" and sys.argv[1] != "--df":
print help
sys.exit()
try:
file = open(sys.argv[2])
cleanstring = file.read()
file.close()
except:
print "I cant read the given file."
sys.exit()
keystr=sys.argv[3]
abc="abcdefghijklmnopqrstuvwxyz"
def eltolas(z):
#az abc eltolasanak muvelete
global abc
ujabc=abc
while z:
ujabc = ujabc[1:] + ujabc[0]
z-=1
return ujabc
def viggen(abc):
#vigenere kodtabla generalasa az adott abc-bol
viggent={}
for x in range(len(abc)):
for y in range(len(abc)):
viggent[abc[x]+":"+abc[y]] = eltolas(x)[y]
return viggent
def orderit(string):
#az adott string kriptografiailag tisztabba tetele
string=string.lower()
ujstr=""
for k in range(len(string)):
if string[k] not in abc:
pass
else:
ujstr = ujstr+string[k]
return ujstr
def titkosit(clean,kulcs,vigtabla):
clean=orderit(clean)
hu=0
while len(kulcs) != len(clean):
if hu>len(kulcs): hu=0
kulcs = kulcs+kulcs[hu]
hu+=1
codedstr=""
for f in range(len(clean)):
codedstr=codedstr + vigtabla[clean[f]+":"+kulcs[f]]
return codedstr
def uncipher(coded,kulcs,vigtabla):
clean=orderit(coded)
hu=0
while len(kulcs) != len(clean):
if hu>len(kulcs): hu=0
kulcs = kulcs+kulcs[hu]
hu+=1
codedstr=""
aktl=[]
for f in range(len(clean)):
for o in abc:
if vigtabla[o + ":" + kulcs[f]] == coded[f]: aktl.append(o)
for lett in aktl:
codedstr=codedstr+lett
return codedstr
#print "A nyilt szoveg: " + cleanstring
if sys.argv[1] == "--e":
coded = titkosit(cleanstring,keystr,viggen(abc))
print coded
elif sys.argv[1] == "--ef":
coded = titkosit(cleanstring,keystr,viggen(abc))
file = open(efname, 'wb')
file.write(coded)
file.close()
print "Your file has been succesfully saved as '" + efname + "'."
elif sys.argv[1] == "--d":
clean= uncipher(cleanstring,keystr,viggen(abc))
print clean
elif sys.argv[1] == "--df":
clean= uncipher(cleanstring,keystr,viggen(abc))
file = open(dfname,'wb')
file.write(clean)
file.close()
print "Your file has been succesfully saved as '" + dfname + "'."