Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Cryptography
Views: 62
#Question 6 S1=[[1,0,1],[0,1,0],[0,0,1],[1,1,0],[0,1,1],[1,0,0],[1,1,1],[0,0,0], [0,0,1],[1,0,0],[1,1,0],[0,1,0],[0,0,0],[1,1,1],[1,0,1],[0,1,1]] S2=[[1,0,0],[0,0,0],[1,1,0],[1,0,1],[1,1,1],[0,0,1],[0,1,1],[0,1,0], [1,0,1],[0,1,1],[0,0,0],[1,1,1],[1,1,0],[0,1,0],[0,0,1],[1,0,0]]
def Expander(L): #Expands a string from 6 bits to 8 bits for SDES if not len(L)==6: print "Wrong length" return 0 R = [L[0],L[1],L[3],L[2],L[3],L[2],L[4],L[5]] return R
def xor(L,M): #performs a binary xor (addition) of the two strings L,M if not len(L)==len(M): print "Strings have different lengths" return 0 return [sum(a)%2 for a in zip(L,M)]
def roundkey(K,i): #Return the 8 bit key for round i from the master 9-bit key if i==1 or i==2: return K[i-1:i+7] else: return K[i-1:10]+K[0:i-2]
def bin2int(L): #convert binary list to integer sum=0 l=len(L)-1 for i in [0..l]: if L[i]==1: sum+=2^(l-i) return sum
def split(M): #Splits the list M into two equal halves, used several times in SDES algorithm if len(M)%2==1: print "Wrong Message Length" return (M[0:len(M)/2],M[len(M)/2:len(M)])
def SDESf(R,Ki): #Performs the function f that is used in the encryption of SDES E=Expander(R) Temp=xor(E,Ki) (rhalf,lhalf)=split(Temp) return S1[bin2int(rhalf)]+S2[bin2int(lhalf)]
def SDESround(M,Kr): #Performs one round of SDES using the roundkey Kr (L1,R1)=split(M) L2=R1 R2 = xor(SDESf(R1,Kr),L1) return L2+R2
def SDES(M,K,r): #Performs SDES on M with the key K using r rounds. for i in [1..r]: rkey=roundkey(K,i) M=SDESround(M,rkey) print "Round:",i,M return M
#Example: SDES([1,0,0,1,0,0,1,0,0,1,0,0],[1,1,1,1,1,1,1,1,0],7)
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 996, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'SDES' is not defined