# Lab 5: Hillcipher. Problem 1. # Compute the following modulo 26. # (a) # 17+24 # (b) # 20 x 5 # (c) # 7*(Vector(3, {(1) = 4, (2) = 12, (3) = 21}))-3*(Vector(3, {(1) = 14, (2) = 5, (3) = 16})); # (d) # Typesetting[delayDotProduct](Matrix(3, 3, {(1, 1) = 11, (1, 2) = 22, (1, 3) = 14, (2, 1) = 7, (2, 2) = 9, (2, 3) = 21, (3, 1) = 17, (3, 2) = 0, (3, 3) = 3}), Matrix(3, 3, {(1, 1) = 3, (1, 2) = 10, (1, 3) = 20, (2, 1) = 20, (2, 2) = 9, (2, 3) = 17, (3, 1) = 9, (3, 2) = 4, (3, 3) = 17}), true); # Problem 1 Answers: (a) 15 (b) 22 # (c) 7*<4,12,21>-3*<14,5,16> mod 26; # equals <12,17,21>; # (d) A:=Matrix([[11,22,14],[7,9,21],[17,0,3]]); # B:=Matrix([[3,10,20],[20,9,17],[9,4,17]]); # A.B mod 26; equals the identity matrix with(LinearAlgebra): # Hillcipher Lab 5: Problem 2. # Encode the phrase BUY TEN SHARES TOMORROW # using the key matrix A from the Examples. # # Confusion results from using index 1..26 in the alphabet, but # index 0 to 25 for these letters in the 3x7 matrix. The coding # below tries to hide the off-by-1 errors by writing 3 functions # to encapsulate the indexing issues. AZ:="ABCDEFGHIJKLMNOPQRSTUVWXYZ": indexAZ:=X -> searchtext(X,AZ)-1: # index=0..25 ; listOfDigits:=s -> [seq(indexAZ(s[i]),i=1..length(s))]:# s=string ; listOfDigitsDECODE:=L->[seq(AZ[1+L[i]],i=1..numelems(L))]: msg:="BUYTENSHARESTOMORROW": L1:=listOfDigits(msg);numelems(L1); listOfDigitsDECODE(L1); # test decoding ; C1:=Matrix(7,3,L1)^+; # Matrix(3,7,L1) is a common error ; A:=<3,10,20|20,9,17|9,4,17>^+; # Encoding matrix ; Determinant(A) mod 13; Determinant(A) mod 2 ; AC1:=(A.C1) mod 26; L2:=[seq(seq(AC1[i,j],i=1..3),j=1..7)]; listOfDigitsDECODE(L2); # Encoded message ; A1:=(1/A) mod 26; # Decoding matrix ; A2:=(A1.AC1) mod 26; # Decode the encoded message ; L3:=[seq(seq(A2[i,j],i=1..3),j=1..7)]; listOfDigitsDECODE(L3); # Hillcipher Lab 5. Problem 3. # Decode the phrase KSKBHXKDYRVTKRZTQE which was encoded # using the key matrix A from the Examples. msg:="KSKBHXKDYRVTKRZTQE": # Hillcipher Lab 5. Problem 4. # Determine which matrices are invertible modulo 26. m4a := Matrix([[11,20,20],[2,1,24],[9,3,3]]); Determinant(m4a) mod 13; Determinant(m4a) mod 2; m4b := Matrix([[2,5,0],[22,9,4],[17,21,8]]); # unfinished m4c:=Matrix([[3,1,24],[20,11,25],[12,4,19]]); # unfinished # Hillcipher Lab 5. Problem 5. # Consider the key matrix B:=Matrix([[4,9,15],[15,17,6],[24,0,17]]); # (a) # Encode the message MARY HAD A LITTLE LAMB using this key matrix. msg:="MARY HAD A LITTLE LAMB": # (b) # Show that the matrix B is invertible. # (c) # Find 1/B; modulo 26. # (d) # Decode the message FVRMTGJTJJRMULSIRGBEMRNVFRC which was encoded # using the key matrix B. msg:="FVRMTGJTJJRMULSIRGBEMRNVFRC": # Hillcipher Lab 5. Problem 6. # Consider the key matrix C:=Matrix([[11,20,20],[2,1,24],[9,3,3]]); # (a) # Encode the message RED SKY AT NIGHT using this key matrix. msg:="RED SKY AT NIGHT": # (b) # Show that the matrix C is invertible. # (c) # Find 1/C; modulo 26. # (d) # Decode the message IWGEJLFWRBUEUOWBHPZMLMXNXUBOEUAHG which was encoded using the key matrix C. msg:="IWGEJLFWRBUEUOWBHPZMLMXNXUBOEUAHG":