#OK to post homework #GLORIA LIU, Mar 29 2024, Assignment 18 read `C18.txt`: #=====================================# #1. Write a procedure #MakeCubic(L,n) #that inputs a binary string of length 20 and outputs the cubic polynomial in n #BinToIn([op(1..5,L)]+BinToIn([op(6..10,L)]*n+BinToIn([op(11..15,L)]*n^2+BinToIn([op(16..20,L)]*n^3 MakeCubic:=proc(L,n) BinToIn([op(1..5,L)])+BinToIn([op(6..10,L)])*n+BinToIn([op(11..15,L)])*n^2+BinToIn([op(16..20,L)])*n^3: end: #=====================================# #2. Write a procedure #EncodeDES(M,K,r) #that inputs a message M in binary (M has to be of even length) (the usal DES has M is of lenght 64),a #key of length 20*r (a random binary string only known to you and the recipient), uses the key to #generate r cubic functions (the usual DES has r=16, but the key-length is always 56, and it does not #use cubics) uses the key to generate a list of r cubic polynomials [F1,...,Fr], and then applies the #Feistel procedure r times, (composing F1, F2, ..., Fr) to the message M. EncodeDES:=proc(M,K,r) local pList,p,n,i,M1: if nops(M) mod 2 <> 0 then RETURN(FAIL): fi: if nops(K) <> r*20 then RETURN(FAIL): fi: pList:=[seq(MakeCubic([op((20*(i-1)+1)..20*i,K)],n), i=1..r)]: M1:=M: for p in pList do M1:=Feistel(M1,unapply(p,n)): od: M1: end: #=====================================# #3. Write a procedure #DecodeDES(M,K,r) #Such that for ANY key of length 20*r, and any message M #DecodeEDES(EncodeDES(M,K,r),K,r)=M DecodeDES:=proc(M,K,r) local pList,p,n,i,M1: if nops(M) mod 2 <> 0 then RETURN(FAIL): fi: if nops(K) <> r*20 then RETURN(FAIL): fi: pList:=[seq(MakeCubic([op((20*(r-i)+1)..20*(r-i+1),K)],n), i=1..r)]: M1:=M: for p in pList do M1:=revFeistel(M1,unapply(p,n)): od: M1: end: #Testing the procedures for i from 1 to 10 do mLength:=floor(rand(10..100)()/2)*2: M:=[seq(rand(0..1)(), i=1..mLength)]: r:=rand(10..100)(): K:=[seq(rand(0..1)(), i=1..(20*r))]: M1:=EncodeDES(M,K,r): M2:=DecodeDES(M1,K,r): #print(M); #print(M1); #print(M2); #This should print true print(evalb(M2=M)); od: