########### CSC 15 Lab 8b ### You are to write a program that uses the functions below, as well as ### the functions for scrambling and descrambling a message that you wrote ### yourself, that connects to the professor's server at the specified ### ip address and port and send the professor a (nice) message. The ### professor will send back a big, encrypted string that you will need to ### decrypt (hopefully using the solution to the second part of the lab, ### that requires the use of two permutations). There are also challenge ### problems at the end. ### This program allows you to connect to the Professor's server and download # a string message. The prof's ip and port will be given to you from socket import * # download message from tcp/ip server at serverip:serverport; returns string # note the serverip is a STRING and serverport is a number. # message is the string that you want to send to the professor. def download(serverip,serverport,message): cfd = socket(AF_INET, SOCK_STREAM) # create comm socket cfd.connect((serverip,serverport)) # connect to server cfd.send(message); # send message to server s = "" # string to be assembled r = "abc" # something not empty while r: # this means while r is not "null" r = cfd.recv(1024) # read up to 1K if (r): s = s+r # read loop cfd.close() # close socket return s # return string read from socket ##download ### sample usage: # s = download("147.4.180.21",10022,"Give me an A!") # print s ### The above server is only accessible from within Hofstra University. ## for reading/writing to file def readfile(filename): fd = open(filename,"r"); F = fd.read(); # reads entire file into a string fd.close() return F # returns string #readfile # write string S to (string) file def writefile(S,filename): fd = open(filename,"w") fd.write(S) fd.close() # nothing is returned: calls to writefile are statements #writefile # reads permutation from file, returns array representation def readkey(fname): P = [] fd = open(fname,"r") # opens file for reading S = "dfs" # some non-empty string while S!= "": # S will be "" at end of file S = fd.readline() # read one line if S!="": P.append( int(S) ) # ends while loop fd.close() return P # returns array representing a permutation # readkey ####### SAMPLE USAGE (without decryption) m = raw_input("enter message to send to prof: ") s = download("127.0.0.1",10022,m) writefile(s,"downloaded.txt") print "MESSAGE: ===\n", s ############################# MEGACHALLENGE ############################## # Permutations form an important component of many encryption algorithms, but # are not enough by themselves. While it's impractical to enumerate all # possible permuations: there are n! permutations of size n, it becomes # much easier to guess the permutation if the hacker can manage to obtain # a sample ciphertext and the original, unencrypted message that it corresponds # to. For example, if the original message is "hello" and the ciphertext is # "ehlol", then there are only two possible permutations: [1,0,2,4,3], and # [1,0,4,2,3]. You are to write a program to print out ALL possible # permutations that would allow one string to be scrambled into a another # string, then use it to crack the first version of the scramble function. ############################# GIGACHALLENGE ############################## # But cracking scramble2 (using also the lettermap permutation) # is much harder with only one sample message-ciphertext pair, but becomes # simpler if we obtain two or more such samples. Write programs that will # automatically detect common patterns in the samples which can help you # to also guess correctly the lettermap permutation.