#SBTX QWRXLVQVE v0.1 #Python 2.7.X #May 14th 2021 # #Operates on text files #Currently does not have robust error-detection #Also not particularly fast or efficient #It doesn't handle special characters well either #Nor does it play nice with multiple encodings #Overwriting files is out too def alphabetsoup(imp): #Derive a string holding our translation pattern alphalist="ABCDEFGHIJKLMNOPQRSTUVWXYZ" alphalist2=[] for letter in imp: if letter in alphalist or letter in alphalist2: continue else: alphalist2.append(letter) alphalist2.sort() alphalist=alphalist+"".join(alphalist2) return alphalist def spacefinder(imp): #Find where all the spaces are, then reverse their locations spacelist=[] for i in range(len(imp)): if imp[i]==" ": i=(i*(-1)) spacelist.append(i) return spacelist def encode(imp): spacelist=spacefinder(imp) newimp=list(imp.replace(" ","").upper()) alphalist=alphabetsoup(newimp) for i in range(len(newimp)): if i%2==0:#Spaghetti code to translate up or down based on the characters position in the string newimp[i]=alphalist[(alphalist.index(newimp[i])+(i))%(len(alphalist))] else: newimp[i]=alphalist[(alphalist.index(newimp[i])-(i))%(len(alphalist))] for item in spacelist: newimp.insert(item," ") return "".join(newimp) def decode(imp): spacelist=spacefinder(imp) newimp=list(imp.replace(" ","").upper()) alphalist=alphabetsoup(newimp) for i in range(len(newimp)): if i%2==0: newimp[i]=alphalist[(alphalist.index(newimp[i])-(i))%(len(alphalist))] else: newimp[i]=alphalist[(alphalist.index(newimp[i])+(i))%(len(alphalist))] for item in spacelist: newimp.insert(item," ") return "".join(newimp) #Accept work inputs teckstfile=raw_input("Which file would you like to access?\n") opcoderino=raw_input("Encode (E) or Decode (D)?\n") #Develop openvars tf_open=open(teckstfile) tf_work=(teckstfile.split(".")) tf_work.insert(-1,"_d.") tf_work=open("".join(tf_work),"w") #Actually do something for line in tf_open: if opcoderino=="E": line=line.strip() tf_work.write(encode(line)+"\n") if opcoderino=="D": line=line.strip() tf_work.write(decode(line)+"\n") tf_open.close() tf_work.close()