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)); else nsent := cat(nsent, substring(sentence, i)); nsent := cat(nsent, " "); fi: od: end: coltranssimp := proc(message, type, nc, pad) local mess, nr, S, i, k, j, r, q; mess := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](message)); nr := ceil(length(mess)/nc); S := Matrix(nr,nc); ArrayTools[Fill]("", S); mess := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](message)); if type = "encipher" then if pad = "yes" then while length(mess) mod nc <> 0 do mess := cat(mess, "X"); od: fi: k := 0; for i from 1 to nr do for j from 1 to nc do k := k + 1: S[i,j] := substring(mess, k); od: od: S := cat(op(convert(convert(S, Vector), list))); else k := 0; r := irem(length(mess),nc); q := iquo(length(mess),nc); for j from 1 to r do for i from 1 to q+1 do k := k + 1: S[i,j] := substring(mess, k); od: od: for j from r+1 to nc do for i from 1 to q do k := k + 1: S[i,j] := substring(mess, k); od: od: S := cat(op(convert(convert(S, Vector[row]), list))); fi: end: coltranskey := proc(message, type, keyw, pad) local mess, key, keyc, nr, nc, S, F, i, k, j, r, q; mess := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](message)); key := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](keyw)); nc := length(key); nr := ceil(length(mess)/nc); S := Matrix(nr,nc); ArrayTools[Fill]("", S); mess := StringTools[Select](StringTools[IsUpper], StringTools[UpperCase](message)); F := (x, y) -> x[2] <= y[2]; keyc := ListTools:-Enumerate(convert(key, list)); keyc := [seq(sort(keyc, F)[i][1], i = 1..nops(keyc))]; if type = "encipher" then if pad = "yes" then while length(mess) mod nc <> 0 do mess := cat(mess, "X"); od: fi: k := 0; for i from 1 to nr do for j from 1 to nc do k := k + 1: S[i,j] := substring(mess, k); od: od: S := Matrix([LinearAlgebra:-Column(S, keyc)]); S := cat(op(convert(convert(S, Vector), list))); else k := 0; r := irem(length(mess),nc); q := iquo(length(mess),nc); for j from 1 to nops(keyc) do if keyc[j] <= r then for i from 1 to q+1 do k := k + 1: S[i,keyc[j]] := substring(mess, k); od: else for i from 1 to q do k := k + 1: S[i,keyc[j]] := substring(mess, k); od: fi: od: S := cat(op(convert(convert(S, Vector[row]), list))); fi: end: ksetup := proc(type) if type = "Columnar(simple)" then Maplets:-Tools:-Set('KEYW'('enabled') = 'false', 'KEYW'('background') = 'white', 'KEYW'('value') = "None Needed"); Maplets:-Tools:-Set('KEYWL'('enabled') = 'true', 'KEYWL'('background') = 'white', 'KEYWL' = ""); else Maplets:-Tools:-Set('KEYW'('enabled') = 'true', 'KEYW'('background') = 'white', 'KEYW' = ""); Maplets:-Tools:-Set('KEYWL'('enabled') = 'false', 'KEYWL'('background') = 'white','KEYWL'('value') = "None"); fi: end: transcipher := proc(tmess, kl, keyw, type, ctype, pad) local ct; if type = "Columnar(simple)" then ct := coltranssimp(StringTools:-SubstituteAll(tmess, "’", "'"), ctype, kl, pad); else ct := coltranskey(StringTools:-SubstituteAll(tmess, "’", "'"), ctype, StringTools:-SubstituteAll(keyw, "’", "'"), pad); fi: if ctype = "encipher" then convert(inspace(ct, 5), symbol); else convert(ct, symbol); fi: end: wsetup := proc(type) if type = "encipher" then Maplets:-Tools:-Set('TB1'('editable') = 'true', 'TB1'('foreground') = 'blue', 'TB2'('foreground') = 'red', 'DPAD'('enabled') = 'true', 'B1'('enabled') = 'true', 'B1'('foreground') = 'blue', 'B1'('caption') = "Encipher Message"); elif type = "decipher" then Maplets:-Tools:-Set('TB1'('editable') = 'true', 'TB1'('foreground') = 'red', 'TB2'('foreground') = 'blue','DPAD'('enabled') = 'false', 'DPAD'('value') = 'no', 'B1'('enabled') = 'true', 'B1'('foreground') = 'red', 'B1'('caption') = "Decipher Message"); fi: end: clearvar := proc() Maplets:-Tools:-Set('TB1' = ""); Maplets:-Tools:-Set('TB2' = ""); Maplets:-Tools:-Set('TB1'('editable') = 'false'); Maplets:-Tools:-Set('KEYW' = ""); Maplets:-Tools:-Set('KEYWL' = ""); Maplets:-Tools:-Set('KEYW'('enabled') = 'false'); Maplets:-Tools:-Set('KEYWL'('enabled') = 'false'); Maplets:-Tools:-Set('KEYW'('background') = 'gray'); Maplets:-Tools:-Set('KEYWL'('background') = 'gray'); Maplets:-Tools:-Set('DDST'('value') = "?"); Maplets:-Tools:-Set('B1'('caption') = " "); Maplets:-Tools:-Set('B1'('enabled') = 'false'); Maplets:-Tools:-Set('DDB1'('value') = "?"); Maplets:-Tools:-Set('DDB1'('enabled') = 'false'); Maplets:-Tools:-Set('DPAD'('value') = "?"); Maplets:-Tools:-Set('DPAD'('enabled') = 'false'); end: transmaplet := Maplet( Window( 'title'="Transposition Cipher", BoxColumn('halign' = 'none', 'background' = 'navy', BoxRow('halign' = 'none', Label("Transposition cipher type?", 'font' = Font("arial", 17)), DropDownBox['DDST']( 'value' = "?", ["?", "Columnar(simple)", "Columnar(keyword)"], 'height' = 25, 'font' = Font("times", 15), onchange = Action(Evaluate('function' = "ksetup", Argument('DDST', 'quotedtext' = 'true')), SetOption('DDB1'('enabled') = 'true') )), BoxColumn([Label(" Number of Columns? ", 'font' = Font("arial", 17)), TextBox['KEYWL'](1..5, 'value' = "", 'font' = Font("times", bold, 16), 'enabled' = 'false', 'background' = 'gray'), " "], [Label(" Keyword? ", 'font' = Font("arial", 17)), TextBox['KEYW'](1..15, 'value' = "", 'font' = Font("times", bold, 16), 'enabled' = 'false', 'background' = 'gray')]), seq(HorizontalGlue(), i = 1..30)), BoxRow('halign' = 'none', Label("Encipher or Decipher?", 'font' = Font("arial", 17)), DropDownBox['DDB1']( 'value' = "?", ["?", "encipher", "decipher"], 'enabled' = 'false', 'font' = Font("times", 15), onchange = Action(Evaluate('function' = "wsetup", Argument('DDB1', 'quotedtext' = 'true'))) ), Label(" Pad Message with Extra Characters? ", 'font' = Font("arial", 17)), DropDownBox['DPAD']( 'value' = "?", ["?", "yes", "no"], 'font' = Font("times", 15), 'enabled' = 'false' ), Button("What does this mean?", 'background' = 'green', RunDialog('MP')) ), BoxRow('halign' = 'none', Label("Input Message:", 'font' = Font("arial", 17)), TextBox['TB1'](7..60, 'font' = Font("times", bold, 16), 'editable' = 'false'), HorizontalGlue() ), BoxRow('halign' = 'left', Button['B1'](" ", 'enabled' = 'false', 'font' = Font("arial", bold, 15), Evaluate('TB2' = "transcipher", Argument('TB1', 'quotedtext' = 'true'), 'KEYWL', Argument('KEYW', 'quotedtext' = 'true'), Argument('DDST', 'quotedtext' = 'true'), Argument('DDB1', 'quotedtext' = 'true'), Argument('DPAD', 'quotedtext' = 'true') ) ), TextBox['TB2']( 'editable' = 'false', 7..55, 'font' = Font("times", bold, 16) ) ), BoxRow('halign' = 'center', 'background' = 'navy', Button( "Clear", 'font' = Font("arial", bold, 15), Evaluate('function' = 'clearvar()') ), Button( "Quit", 'font' = Font("arial", bold, 15), Shutdown() ) ) ) ), MessageDialog['MP']("Message will be padded with extra characters (X's) so that tranposition encipherment has equal column lengths") ): Maplets[Display]( transmaplet ): # # # # # # # # # # # # # # # # # #