# Carson Chen, Huiting Fan, Xiaobei Liu # Final Project import string import sys dict1={'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} dict2={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'} approvedKey = [str(0),str(1),str(2),str(3),str(4),str(5),str(6),str(7),str(8),str(9),'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] file1 = "apple.txt" file2 = "encoded.txt" file3 = "decoded.txt" key1 = "haha" key2 = "Hello" key3 = "Ihave3points" key4 = "l'g" #----------------------------Primary functions---------------------------------# def encodeFile(filein,fileOut,keyPhrase,method): if str(method) == str(1): print('You are using method 1') unencryptedText = prepareFile(filein) print('processing...') fileToWrite=open(fileOut,'w') key = keyConvert1(keyPhrase) encryptedText=[] for words in unencryptedText: encryptedWord = encodeMethod1(words,key) fileToWrite.write(encryptedWord+' ') fileToWrite.close print('All done!') elif str(method) == str(2): print('You are using method 2') unencryptedString = prepareFile2(filein) print('processing...') fileToWrite=open(fileOut,'w') stringLength = len(unencryptedString) key = keyConvert2(stringLength,keyPhrase) encryptedString=encodeMethod2(unencryptedString,key) fileToWrite.write(encryptedString) fileToWrite.close print('All done!') else: print('This method is not defined') def decodeFile(filein,fileOut,keyPhrase,method): if str(method) == str(1): encryptedText1 = prepareFile(filein) print('You are decoding the message using method 1') print('processing...') fileToWrite=open(fileOut,'w') key = keyConvert1(keyPhrase) decryptedText=[] for words in encryptedText1: decryptedWord1 = decodeMethod1(words,key) fileToWrite.write(decryptedWord1+' ') fileToWrite.close print('All done!') elif str(method) == str(2): encryptedText2 = prepareFile2(filein) print('You are decoding the message using method 2') print('processing...') fileToWrite=open(fileOut,'w') lengthFile = len(fileToWrite) key = keyConvert2(lengthFile,keyPhrase) decryptedString = decodeMethod2(words,key) decryptedStringSplitted=decryptedString.split('|') for decryptedWord2 in decryptedStringSplitted fileToWrite.write(decryptedWord2+' ') fileToWrite.close print('All done!') else: print('This method is not defined') #----------------------------Secondary functions-------------------------------# def encodeMethod1(message,key): '''takes in a word and a key, and convert the word to a string according to the first algorithm of parallel shift''' list2 = [] for ch in message: ch_lower = ch.lower() if ch_lower in list(dict1.keys()): num = numConvert(ch_lower) newNum = num + key if newNum <= 26: newch = dict2[newNum] list2.append(newch) elif num + key >26: newNum1 = newNum-26 newch = dict2[newNum1] list2.append(newch) else: list2.append(int(ch)) list2length = len(list2) stringWord = '' for i in range(list2length): stringWord += list2[i] return stringWord def decodeMethod1(message,key): '''The inverse function of encodeMethod1''' decodeKey = 26-int(key) return(encodeMethod1(message,decodeKey)) def keyConvert1(word): '''takes in a phrase and covert it into a number key from 1-26''' list1=[] for ch in word: ch_lower = ch.lower() if ch_lower in list(dict1.keys()): num = numConvert(ch_lower) list1.append(num) else: list1.append(int(ch_lower)) key=sum(list1)%26+1 return key def encodeMethod2(message,key): '''takes in a word and a key, and convert the word to a string according to the second algorithm of anagram''' messageLower = message.lower() finalString = "" for i in range(key): condensedString = condenseString(messageLower,key,i) finalString = finalString + condensedString return finalString def decodeMethod2(message,key): "map to k(l//n)+0,1,2..." l = len(message) n = int(key) k = l//n+1 outputString = "" positionTrack = [] for i in range(0,l): inputPosition = (i*k)%l if str(inputPosition) not in positionTrack: print str(message[inputPosition]) outputString += str(message[inputPosition]) positionTrack.append(str(inputPosition)) elif inputPosition not in positionTrack: print 'wrong' "Do nothing" return outputString test1 = encodeMethod2('Mary|has|a|little|sheep',4) print test1 test2 = decodeMethod2(test1,4) print test2 def keyConvert2(length,word): '''takes in a word, and the length of message as reference, and covert the word into a number key from 2 to 27''' list1=[] for ch in word: ch_lower = ch.lower() if ch_lower in list(dict1.keys()): num = numConvert(ch_lower) list1.append(num) else: list1.append(int(ch_lower)) if length >= 26: key=sum(list1)%26+2 return key else: key=sum(list1)%length+2 return key #------------------------------Basic functions---------------------------------# def prepareFile(file): '''takes in a file and change it into a list''' originalFile=open(file,'r') text=originalFile.read() textSplitted=text.split() originalFile.close() return textSplitted def prepareFile2(file): '''takes in a file and change it into a string, with all spaces replaced by sign '|', which is useful in mode 2 and mode 3''' listFile2 = prepareFile(file) file2String = "" for i in range(len(listFile2)): file2String = file2String+listFile2[i]+"|" return file2String def makeMultiple(inputString,n): # Not in use! '''takes in a string, and add"|" to the string until the length of the string is a multiple of the input number, n''' while len(inputString) % int(n) != 0: inputString = inputString + "|" return inputString def condenseString(inputString,n,ini): '''takes in inputString and a number, and the initial position, and outputs another string that contains all the characters at position n,2n,3n... of the inputString''' pos = ini%n outputString="" for i in range(len(inputString)): outputString += inputString[pos] pos += n if pos > len(inputString)-1: return outputString else: "Do nothing" def reduceString(inputString,n): # Not in use! '''the complement function of condenseString, it takes in inputString and a number, outputs another string that contains ALL BUT the characters at position n,2n,3n... of the inputString''' pos = 0 outputString="" for i in range(len(inputString)): if i%n != 0: outputString += inputString[pos] elif i%n == 0: "Do nothing" pos += 1 if pos > len(inputString)-1: return outputString else: "Do nothing" def keyCheck(word): # Not in use! '''takes in the key and check whether it is legitimate''' characters = word.lower() for ch in characters: if ch not in approvedKey: print("Please use only numbers and letters, no spaces") return False else: "Do nothing" print("keycheck passed") return True def numConvert(character): '''takes in the character and finds the corresponding number''' num=dict1[character] return(num)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question