# title£ºEncrypt and Decrypt messages with a new way # purpose: the code for Task2.2 & 2.3 # author: Siyu Chen # Date: 2021/12/1 # N.B. : Some steps can be merged to one step,but I retain them to show my ideas more clearly. # I really hope I don't confuse you. #Task2.2 # create matrix, define the amounts of rows and columns Polybius<-matrix(nrow=4,ncol=10) # input the elements of per rows Polybius[1,]<-c(NA,'Z','M','D',NA,'K','W','Q',NA,'G') Polybius[2,]<-c('V',NA,'A',NA,'R',NA,'E',NA,'T','O') Polybius[3,]<-c(NA,'J','I','S','B','P','Y',NA,'F',NA) Polybius[4,]<-c('L',NA,NA,'C',NA,'U','H','N',NA,'X') # create numerical labels of rows and columns rownames(Polybius)<-c(0:3) colnames(Polybius)<-c(0:9) # display the matrix Polybius # Encrypt keyword keyword_temp<-NULL for(k in 1:5){ c<-substring('PEACH',k,k) # extract character one by one for(i in 1:4){ # i replace row for (j in 1:10){ # j replace column if(!is.na(Polybius[i,j])){ # filter the NA if(Polybius[i,j]==c){ keyword_temp<-c(keyword_temp,i-1) keyword_temp<-c(keyword_temp,j-1) } } } } } keyword_temp # convert keyword_temp to numbers of each character keyword_temp_x=NULL keyword_temp_y=NULL for(i in seq(keyword_temp)){ if(!i%%2==0){ keyword_temp_x<-c(keyword_temp_x,keyword_temp[i]) }else{ keyword_temp_y<-c(keyword_temp_y,keyword_temp[i]) } } keyword_encrypt<-10*keyword_temp_x+keyword_temp_y keyword_encrypt # Encrypt message ciphertext_temp1<-NULL for(k in 1:12){ c<-substring('RAISETHEFLAG',k,k) # extract character one by one for(i in 1:4){ # i replace row for (j in 1:10){ # j replace column if(!is.na(Polybius[i,j])){ # filter the NA if(Polybius[i,j]==c){ ciphertext_temp1<-c(ciphertext_temp1,i-1) ciphertext_temp1<-c(ciphertext_temp1,j-1) } } } } } ciphertext_temp1 # convert ciphertext_temp1 to numbers of each character ciphertext_temp1_x=NULL ciphertext_temp1_y=NULL for(i in seq(ciphertext_temp1)){ if(!i%%2==0){ ciphertext_temp1_x<-c(ciphertext_temp1_x,ciphertext_temp1[i]) }else{ ciphertext_temp1_y<-c(ciphertext_temp1_y,ciphertext_temp1[i]) } } ciphertext_temp2<-10*ciphertext_temp1_x+ciphertext_temp1_y ciphertext_temp2 # encrypt with keyword_encrypt ciphertext_temp3<-ciphertext_temp2+keyword_encrypt ciphertext_temp3 # reconvert ciphertext_temp3 to each digit ciphertext_temp3_x=NULL ciphertext_temp3_y=NULL ciphertext_temp4=NULL for(i in seq(ciphertext_temp3)){ ciphertext_temp3_x<-c(ciphertext_temp3_x,ciphertext_temp3[i]%/%10) ciphertext_temp4<-c(ciphertext_temp4,ciphertext_temp3_x[i]) ciphertext_temp3_y<-c(ciphertext_temp3_y,ciphertext_temp3[i]%%10) ciphertext_temp4<-c(ciphertext_temp4,ciphertext_temp3_y[i]) } ciphertext_temp4 # add " " count=0 ciphertext_temp5<-NULL for(i in 1:length(ciphertext_temp4)){ if(count!=0 & (count)%%5==0) ciphertext_temp5<-c(ciphertext_temp5,' ') ciphertext_temp5<-c(ciphertext_temp5,ciphertext_temp4[i]) count<-count+1 } ciphertext_temp5 # fill the final block up to 5 digits ciphertext<-NULL if(length(ciphertext_temp5)%%6!=0){ num<-5-length(ciphertext_temp5)%%6 x<-sample(0:9,num,replace=TRUE) ciphertext<-c(ciphertext_temp5,x) } # display the encrypted message ciphertext # convert ciphertext to a vector contains one element len=length(ciphertext)%/%6+1 finaltext_temp=NULL finaltext=NULL for(h in 1:len){ finaltext_temp<-paste(ciphertext[(6*h-5):(6*h-1)],collapse="") finaltext<-paste(finaltext,finaltext_temp,collapse=" ") } # display the finaltext print(finaltext) # ------the output of Task2.2 by steps (just for reading ; can be deleted)------ Polybius keyword_temp keyword_encrypt ciphertext_temp1 ciphertext_temp2 ciphertext_temp3 ciphertext_temp4 ciphertext_temp5 ciphertext finaltext #Task2.3 # read ciphertext ciphertext<-c(4,8,5,2,1, 8,2,5,5,9, 5,3,3,2,0, 8,3,1,5,1, 4,4,3,8,2, 1,3,3,4,9, 4,0,3,5,2, 4,2,2,6,0, 4,8,5,2,1, 8,4,3,4,9, 4,8,3,4,3, 2,3,5,1,8 ) # let the length of ciphertext to be even len=length(ciphertext)-(length(ciphertext)%%2) # seperate row-coordinate and column-coordinate ciphertext_x=NULL ciphertext_y=NULL for(i in 1:len){ if(!i%%2==0){ ciphertext_x<-c(ciphertext_x,ciphertext[i]) }else{ ciphertext_y<-c(ciphertext_y,ciphertext[i]) } } ciphertext_temp1<-10*ciphertext_x+ciphertext_y ciphertext_temp1 # decipher the keyword keyword_temp<-NULL for(k in 1:5){ c<-substring('LEMON',k,k) # extract character one by one for(i in 1:4){ # i replace row for (j in 1:10){ # j replace column if(!is.na(Polybius[i,j])){ # filter the NA if(Polybius[i,j]==c){ keyword_temp<-c(keyword_temp,i-1) keyword_temp<-c(keyword_temp,j-1) } } } } } keyword_temp # convert keyword_temp to numbers of each character keyword_temp_x=NULL keyword_temp_y=NULL for(i in seq(keyword_temp)){ if(!i%%2==0){ keyword_temp_x<-c(keyword_temp_x,keyword_temp[i]) }else{ keyword_temp_y<-c(keyword_temp_y,keyword_temp[i]) } } keyword_encrypt<-10*keyword_temp_x+keyword_temp_y keyword_encrypt # decrypt with keyword_encrypt ciphertext_temp2<-ciphertext_temp1-keyword_encrypt ciphertext_temp2 # reconvert ciphertext_temp3 to each digit ciphertext_temp2_x=NULL ciphertext_temp2_y=NULL ciphertext_temp3=NULL for(i in seq(ciphertext_temp2)){ ciphertext_temp2_x<-c(ciphertext_temp2_x,ciphertext_temp2[i]%/%10) ciphertext_temp3<-c(ciphertext_temp3,ciphertext_temp2_x[i]) ciphertext_temp2_y<-c(ciphertext_temp2_y,ciphertext_temp2[i]%%10) ciphertext_temp3<-c(ciphertext_temp3,ciphertext_temp2_y[i]) } ciphertext_temp3 # filter the negative and the following numbers ciphertext_temp4=NULL for(i in seq(ciphertext_temp3)){ if(ciphertext_temp3[i]<0) break ciphertext_temp4<-c(ciphertext_temp4,ciphertext_temp3[i]) } ciphertext_temp4 # seperate row-coordinate and column-coordinate len=length(ciphertext_temp4)-(length(ciphertext_temp4)%%2) ciphertext_temp4_x=NULL ciphertext_temp4_y=NULL for(i in 1:len){ if(!i%%2==0){ ciphertext_temp4_x<-c(ciphertext_temp4_x,ciphertext_temp4[i]) }else{ ciphertext_temp4_y<-c(ciphertext_temp4_y,ciphertext_temp4[i]) } } # decipher the message message_temp=NULL for(i in seq(ciphertext_temp4_x)){ message_temp<-c(message_temp,Polybius[ciphertext_temp4_x[i]+1,ciphertext_temp4_y[i]+1]) } message_temp # convert it to a vector which contains one element that holds the entire message plaintext<-paste(message_temp,collapse="") plaintext # ------the output of Task2.3 by steps (just for reading ; can be deleted)------- ciphertext ciphertext_temp1 keyword_temp keyword_encrypt ciphertext_temp2 ciphertext_temp3 ciphertext_temp4 message_temp plaintext