################ CSC 15 Lab 9: Interactive Descramble ################### # This file contains functions provided by the professor to students. # Place this program in the same directory as your program, and import with # from scrambase import * # DO NOT EDIT THIS FILE, JUST INCLUDE IT IN YOURS. # This file already contains files that reads three permutations, LP, PI # and BP from a file named "key1.txt") (make sure this file is also in the # same directory). ### LP is a permutation of length 128. It permutes the ascci char set ### so that each letter is mapped to another letter. ### PI is a permutation of length 64. It should be used to permute the ### position of the characters in a string. ### BP is a permutation of length 7. It represents a bitwise permutation ### of an 8-bit non-negative value (first bit stays 0). # Study the function "download" below to learn how it is to be used. # This function allows you to send and receive a message, at varying # levels of encryption, interactively with the professor's server over # the network. # The professor's server is running at 147.4.180.39, port 10024. However, # multiple servers may be made available on other ports ### This assignment must be completed on a non-wifi computer on campus ### as Hofstra's wireless routers block the ports that we need. ### Note that the download function takes a parameter indicating the ### encryption level of the message. # level 0 is plain text # level 1 scrambles the positions of the chars (using PI and its inverse) # level 2 is level 1 plus scrambling of ascii char set (using LP) # level 3 is level 2 plus bit-wise scrambling of each 8-bit value. LP is # applied before BP when scrambling. ### Note that the 'message' argument to download need to be encrypted at ### the level indicated. You will receive an crypted message from the ### professor encrypted at the same level, which you will need to decrypt. ### Optionally, you may write what you received to a file using the ### writefile function included here. # Write a function that interactively take info from you, scramble, # then call download to interact with the professor. Be nice to the # professor, who have already provided you enough code for level 1 # encryption (see homepage). ### Please pad your strings with blank spaces for easy reading. from string import join # join(['a','b','c'],"") gives "abc" from socket import * # range(0,n) gives id permutation from 0 to n-1 # returns inverse permutation of P: Q[P[i]] = i def inverse(P): Q = [0]*len(P) i = 0 while i< len(P): Q[P[i]] = i i += 1 # return Q # inverse permutation of P ### This function takes a filename (such as "key1.txt"), reads it and ### returns a triple (LP,PI,BP): ### which are interpreted as follows: ### LP is a permutation of length 128. It permutes the ascci char set ### so that each letter is mapped to another letter. ### PI is a permutation of length 64. It should be used to permute the ### position of the characters in a string. ### BP is a permutation of length 7. It represents a bitwise permutation ### of an 8-bit non-negative value (first bit stays 0). def readkey(filename): fd = open(filename,"r") n = int(fd.readline()) P = [0]*n i = 0 while i3): elevel=0 # force to be plain text if len(message)>=1024: message = message[:1023] el = chr(elevel+48) cfd.send(el) # send level indicator 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 (could be encrypted) ##download ### sample usage: # s = download("147.4.180.39",10024,"Give me a D!",0) # s = download("147.4.180.39",10024,scramble1("Give me a C!"),1) # s = download("147.4.180.39",10024,scramble2("Give me a B!"),2) # s = download("147.4.180.39",10024,scramble3("Give me an A!"),3) # print s # This assumes that scramble1/2/3 are your scramble functions.