restart; with(Maplets[Elements]): inspace := proc(sentence, bl) local i, j, nsent; nsent := ""; for i from 1 to length(sentence) do if (i) mod bl <> 0 then nsent := cat(nsent, substring(sentence, i)); elif (i) mod bl = 0 then nsent := cat(nsent, substring(sentence, i)); if i <> length(sentence) then nsent := cat(nsent, " "); fi: fi: od: nsent; end: csimpletrans := proc(ctext, coln) local q, r, M, i, j, mess, ia, res, pt; mess := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](ctext)); q := iquo(length(mess), coln); r := irem(length(mess), coln); if r > 0 then M := Matrix(q+1, coln, fill = ""); ia := 0; for j from 1 to r do for i from 1 to q+1 do ia := ia + 1; M[i,j] := mess[ia]; od: od: for j from r+1 to coln do for i from 1 to q do ia := ia + 1; M[i,j] := mess[ia]; od: od: else M := Matrix(q, coln, fill = ""); for j from 1 to coln do for i from 1 to q do M[i,j] := mess[i + (j-1)*q]; od: od: fi: res := ""; pt := ""; for i from 1 to LinearAlgebra:-RowDimension(M) do pt := cat(pt, op(convert(LinearAlgebra:-Row(M, i), list))); if i <> LinearAlgebra:-RowDimension(M) then res := cat(res, inspace(cat(op(convert(LinearAlgebra:-Row(M, i), list))), 1), StringTools:-Char(10)); else res := cat(res, inspace(cat(op(convert(LinearAlgebra:-Row(M, i), list))), 1)); fi: od: Maplets:-Tools:-Set('PT' = pt); convert(res, symbol); end: simpletransbreakermaplet := Maplet('onstartup' = 'A1', Window['W1']('title' = "Simple Transposition Cipher Breaker", BoxColumn('halign' = 'none', 'background' = 'navy', BoxRow('halign' = 'none', Label("Ciphertext: " , 'font' = Font("arial", 17)), TextBox['CTEXT']('font' = Font("arial", bold, 16), 'foreground' = 'red', 4..60) ), BoxColumn( BoxRow('halign' = 'none', BoxColumn(seq(VerticalGlue(), i = 1..7), BoxRow(Label("Column #: ", 'font' = Font("arial", 17)), TextBox['CN']('font' = Font("arial", bold, 16), 1..5), HorizontalGlue()), Button['BS']("Decipher Message", 'font' = Font("arial", bold, 15), Action(Evaluate('PTRANS' = "csimpletrans", Argument(CTEXT, 'quotedtext' = 'true'), 'CN') )), seq(VerticalGlue(), i = 1..5), Button("How does \n this work?", 'background' = 'yellow', 'width' = 95, RunDialog('MTRANSB'))), TextBox['PTRANS']('foreground' = 'blue', 'font' = Font("Monospaced", bold, 16), 'wrapped' = 'false', 15..70)), BoxRow('halign' = 'none', Label("Plaintext: " , 'font' = Font("arial", 17)), TextBox['PT']('font' = Font("arial", bold, 16), 'foreground' = 'blue', 'background' = 'white', 4..60) )), BoxColumn('background' = 'navy', BoxRow('background' = 'navy', Button( "Clear All", 'font' = Font("arial", bold, 15), Action(SetOption('CTEXT' = "" ), SetOption('PT' = "" ), SetOption('CN' = "" ), SetOption('PTRANS' = "" ) ) ), " ", Button( "Quit", 'font' = Font("arial", bold, 15), Shutdown()) )) ) ), MessageDialog['MTRANSB']("In order to cryptanalyze and break the simple transposition cipher, the user enters a possible number of columns and attempts to decipher the message. If a legible message is generated, the correct number of columns is found and the message is deciphered.",'type'='information'), Action['A1'](RunWindow('W1')) ): Maplets[Display]( simpletransbreakermaplet ): # # # # #