Over the last year I have been programming on my own, mostly in Python. I haven't had any formal education, though I have followed some online instructions, and have been making things that interest me.
One of those things is my Vigenère encrypter. I've made a few versions of it, and I would appreciate feedback on my latest version. Such as, does it follow somewhat of a good design pattern for these things? How would it perform under heavy load?
If I can provide any additional information, please ask and I won't hesitate to do so.
This is my code:
from os import system
#characters to numbers table
dictDown = {'|':0,'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,
'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,
'q':17,'r':18,'s':19,'t':20,'u':21,'v':22,'w':23,'x':24,
'y':25,'z':26,'.':27,',':28,"'":29,'!':30,':':31,'1':32,
'2':33,'3':34,'4':35,'5':36,'6':37,'7':38,'8':39,'9':40,
'0':41,'=':42,'-':43,'"':44,'+':45,'[':46,'_':47,'(':48,
')':49,' ':50,'?':51,'\n':52,'@':53,'#':54,'/':55,'%':56,
'^':57,'*':58,']':59,'{':60,'}':61,'A':62,'B':63,'C':64,
'D':65,'E':66,'F':67,'G':68,'H':69,'I':70,'J':71,'K':72,
'L':73,'M':74,'N':75,'O':76,'P':77,'Q':78,'R':79,'S':80,
'T':81,'U':82,'V':83,'W':84,'X':85,'Y':86,'Z':87,'<':88,
'>':89,';':90}
#numbers to characters table
dictUp = {0:'|',1:'a',2:'b',3:'c',4:'d',5:'e',6:'f',7:'g',8:'h',
9:'i',10:'j',11:'k',12:'l',13:'m',14:'n',15:'o',16:'p',
17:'q',18:'r',19:'s',20:'t',21:'u',22:'v',23:'w',24:'x',
25:'y',26:'z',27:'.',28:',',29:"'",30:'!',31:':',32:'1',
33:'2',34:'3',35:'4',36:'5',37:'6',38:'7',39:'8',40:'9',
41:'0',42:'=',43:'-',44:'"',45:'+',46:'[',47:'_',48:'(',
49:')',50:' ',51:'?',52:'\n',53:'@',54:'#',55:'/',56:'%',
57:'^',58:'*',59:']',60:'{',61:'}',62:'A',63:'B',64:'C',
65:'D',66:'E',67:'F',68:'G',69:'H',70:'I',71:'J',72:'K',
73:'L',74:'M',75:'N',76:'O',77:'P',78:'Q',79:'R',80:'S',
81:'T',82:'U',83:'V',84:'W',85:'X',86:'Y',87:'Z',88:'<',
89:'>',90:';'}
#encrypting function
def crypt(key,clearText):
cryptText = []
numKey = []
keyCount = 0
#translate the characters in the key to numbers
for keyClearChar in key:
keyNum = dictDown[keyClearChar]
numKey.append(keyNum)
for clearChar in clearText:
#loop characters in key
if keyCount == len(key):
keyCount = 0
charNum = dictDown[clearChar]
keyIndex = numKey[keyCount]
cryptChar = dictUp[charNum+keyIndex%len(dictUp)]
cryptText.append(cryptChar)
keyCount += 1
return ''.join(cryptText)
#inverted encrypting function
def decrypt(key,cryptText):
clearText = []
numKey = []
keyCount = 0
for keyClearChar in key:
keyNum = dictDown[keyClearChar]
numKey.append(keyNum)
for cryptChar in cryptText:
if keyCount == len(key):
keyCount = 0
cryptNum = dictDown[cryptChar]
keyIndex = numKey[keyCount]
clearChar = dictUp[cryptNum-keyIndex%len(dictUp)]
clearText.append(clearChar)
keyCount += 1
return ''.join(clearText)
while True:
choice = raw_input("Enter 'e' to encrypt,\nEnter 'd' to decrypt.\n\n")
if choice.lower() == 'e':
system('cls')
key = list(raw_input("Enter an encryption key here:\n"))
clearText = list(raw_input("\nEnter the text to be encrypted here:\n"))
system('cls')
cryptText = crypt(key,clearText)
cryptDir = open("C:\Users\Yorick\Desktop\cryptText.txt",'w')
cryptDir.write(cryptText)
cryptDir.close()
print("Encryption finished.\nResult saved to desktop.\nPress enter to close the program.")
_=raw_input('')
break
elif choice.lower() == 'd':
system('cls')
key = list(raw_input("Enter a decryption key here:\n"))
#try opening encrypted txt file
try:
cryptDir = open("C:\Users\Yorick\Desktop\cryptText.txt",'r')
cryptText = list(cryptDir.read())
cryptDir.close()
except IOError:
print("No cryptText.txt found on desktop.")
clearText = decrypt(key,cryptText)
print(clearText)
_=raw_input("\n\n\n\nPress enter to close.")
break