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: fmatrc := proc(let, pmat, d) local i, j, res; for i from 1 to d do for j from 1 to d do if pmat[i, j] = let then res := [i, j]; break: fi: od: od: res; 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: adfgvx := proc(mess, ctype, ed, key) local let, gmatrix, res, cmess, rc, i, j, d, ct, s, r, c, amat; amat := Matrix(7,7); for i from 1 to 7 do for j from 1 to 7 do amat[i,j] := Maplets:-Tools:-Get(cat("PT", convert((i-1)*7+j, string))('value')); od: od: if ctype = "ADFGVX" then let := ["A", "D", "F", "G", "V", "X"]; gmatrix := LinearAlgebra:-DeleteRow(amat, 1..1); gmatrix := LinearAlgebra:-DeleteColumn(gmatrix, 1..1); else let := ["A", "D", "F", "G", "X"]; gmatrix := LinearAlgebra:-DeleteRow(amat, [1,7]); gmatrix := LinearAlgebra:-DeleteColumn(gmatrix, [1,7]); fi: res := ""; d := LinearAlgebra:-RowDimension(gmatrix); if ed = "encipher" then if ctype = "ADFGX" then cmess := StringTools:-SubstituteAll(StringTools:-Select(StringTools:-IsUpper, StringTools:-UpperCase(mess)), "J", "I"); else cmess := StringTools:-Select(StringTools:-IsAlphaNumeric,StringTools:-UpperCase(mess)); fi: for i from 1 to length(cmess) do rc := fmatrc(cmess[i], gmatrix, d); res := cat(res, let[ rc[1] ], let[ rc[2] ]); od: res := coltranskey(res, "encipher", key, "no"); else ct := coltranskey(mess, "decipher", key, "no"); for i from 1 to length(ct) by 2 do s := substring(ct, i..i+1); r := ListTools:-Search(s[1], let); c := ListTools:-Search(s[2], let); res := cat(res, gmatrix[r,c]); od: fi: if ed = "encipher" then convert(inspace(res, 5), symbol); else convert(res, symbol); fi: end: matassign := proc(mtype) local i, j; for i from 1 to 7 do for j from 1 to 7 do Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+j, string))('value') = ""); od: od: Maplets:-Tools:-Set("PT1"('background') = 'black', "PT1"('enabled') = 'false'); if mtype = "ADFGX" then Maplets:-Tools:-Set("PT7"('visible') = 'false', "PT7"('editable') = 'false'); Maplets:-Tools:-Set("PT43"('visible') = 'false', "PT43"('visible') = 'false'); Maplets:-Tools:-Set("PT49"('visible') = 'false', "PT49"('visible') = 'false'); for j from 2 to 6 do Maplets:-Tools:-Set(cat("PT", convert(j, string))('value') = mtype[j-1], cat("PT", convert(j, string))('background') = 'green', cat("PT", convert(j, string))('editable') = 'false'); Maplets:-Tools:-Set(cat("PT", convert(42 + j, string))('visible') = 'false'); od: for i from 2 to 6 do Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+1, string))('value') = mtype[i-1], cat("PT", convert((i-1)*7+1, string))('background') = 'green', cat("PT", convert((i-1)*7+1, string))('editable') = 'false'); Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+7, string))('visible') = 'false'); od: elif mtype = "ADFGVX" then Maplets:-Tools:-Set("PT7"('visible') = 'true', "PT7"('editable') = 'true'); Maplets:-Tools:-Set("PT43"('visible') = 'true'); Maplets:-Tools:-Set("PT49"('visible') = 'true', "PT49"('editable') = 'true'); for j from 2 to 7 do Maplets:-Tools:-Set(cat("PT", convert(j, string))('value') = mtype[j-1], cat("PT", convert(j, string))('background') = 'green', cat("PT", convert(j, string))('editable') = 'false'); Maplets:-Tools:-Set(cat("PT", convert(42 + j, string))('visible') = 'true'); od: for i from 2 to 7 do Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+1, string))('value') = mtype[i-1], cat("PT", convert((i-1)*7+1, string))('background') = 'green', cat("PT", convert((i-1)*7+1, string))('editable') = 'false'); Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+7, string))('visible') = 'true'); od: fi: end: checkmat := proc(mtype) local i, j, mset, abet, nstr, mlist, aset; abet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; nstr := "0123456789"; mlist := []; if mtype = "ADFGX" then aset := convert(StringTools:-Explode(abet), set); for i from 2 to 6 do for j from 2 to 6 do aset := aset minus {Maplets:-Tools:-Get(cat("PT", convert((i-1)*7+j, string))('value'))}; od: od: if nops(aset) > 1 then Maplets:-Display(acheckn1); elif nops(aset) = 1 and aset[1] <> "I" and aset[1] <> "J" then Maplets:-Display(acheckn1); else Maplets:-Display(achecky); fi: else aset := convert(StringTools:-Explode(cat(abet,nstr)), set); for i from 2 to 7 do for j from 2 to 7 do aset := aset minus {Maplets:-Tools:-Get(cat("PT", convert((i-1)*7+j, string))('value'))}; od: od: if nops(aset) > 0 then Maplets:-Display(acheckn2); else Maplets:-Display(achecky); fi: fi: end: randmatrix := proc(seed, ctype) local i, j, k, a, r, t, let, lct, ub; r := rand(seed): let := []; if ctype = "ADFGX" then a := "ABCDEFGHIKLMNOPQRSTUVWXYZ"; lct := 25; ub := 6; else lct := 36; a := "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; ub := 7; fi: for i from lct to 1 by -1 do t := a[ (r() mod i) + 1]; let := [op(let), t ]; a := StringTools:-Delete(a, StringTools:-Search(t, a)..StringTools:-Search(t, a)); od: k := 0; for i from 2 to ub do for j from 2 to ub do k := k + 1; Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+j, string))('value') = let[k]); od: od: end: upmatrix := proc(mtype) local i, j, ub; if mytype = "ADFGX" then ub := 6; else ub := 7; fi: for i from 2 to ub do for j from 2 to ub do Maplets:-Tools:-Set(cat("PT", convert((i-1)*7+j, string))('value') = StringTools:-UpperCase(Maplets:-Tools:-Get(cat("PT", convert((i-1)*7+j, string))('value')))); od: od: end: ksetup := proc(type) if type = "Columnar(simple)" then Maplets:-Tools:-Set('KEYW'('enabled') = 'false', 'KEYW'('background') = 'white', 'KEYW'('value') = "None Needed"); else Maplets:-Tools:-Set('KEYW'('enabled') = 'true', 'KEYW'('background') = 'white', 'KEYW' = ""); fi: end: wsetup := proc(type) if type = "encipher" then Maplets:-Tools:-Set('TB1'('editable') = 'true', 'TB1'('foreground') = 'blue', 'TB2'('foreground') = 'red', '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', '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('KEYW'('enabled') = 'false'); Maplets:-Tools:-Set('KEYW'('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'); end: acheckn1 := Maplet(Window('title' = "Warning!", [["Plaintext must contain 25 of the 26 alphabet letters. Check your plaintext array."], [Button("Go Back to ADFGX/ADFGVX Array Window.", 'font' = Font("arial", bold, 15), Shutdown())]] )): acheckn2 := Maplet(Window('title' = "Warning!", [["Plaintext must all of the 26 alphabet letters and 10 numerical digits. Check your plaintext array."], [Button("Go Back to ADFGX/ADFGVX Array Window.", 'font' = Font("arial", bold, 15), Shutdown())]] )): achecky := Maplet(Window('title' = "Check", [["Plaintext array is ok."], [Button("Go Back to ADFGX/ADFGVX Array Window", 'font' = Font("arial", bold, 15), Shutdown())]] )): adfgvxmaplet := Maplet('onstartup' = 'A1', Window['W1']( 'title'="ADFGX/ADFGVX Cipher", BoxColumn('halign' = 'none', 'background' = 'navy', BoxRow('halign' = 'none', Label("Cipher type?", 'font' = Font("arial", 17)), DropDownBox['DDST']( 'value' = "?", ["?", "ADFGX", "ADFGVX"], 'height' = 25, 'font' = Font("times", 15), onchange = Action(Evaluate('function' = "matassign", Argument('DDST', 'quotedtext' = 'true')), RunWindow('W2'), SetOption('DDB1'('enabled') = 'true'), SetOption('KEYW'('enabled') = 'true'), SetOption('KEYW'('background') = 'white') )), BoxColumn([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"], 'font' = Font("times", 15), 'enabled' = 'false', onchange = Action(Evaluate('function' = "wsetup", Argument('DDB1', 'quotedtext' = 'true'))) ), seq(HorizontalGlue(), i = 1..30) ), BoxRow('halign' = 'none', Label("Input Message:", 'font' = Font("arial", 17)), TextBox['TB1'](5..50, 'font' = Font("times", bold, 16), 'editable' = 'false'), " ", HorizontalGlue() ), BoxRow('halign' = 'left', Button['B1'](" ", 'enabled' = 'false', 'font' = Font("times", bold, 12), 'font' = Font("arial", bold, 15), Evaluate('TB2' = "adfgvx", Argument('TB1', 'quotedtext' = 'true'), Argument('DDST', 'quotedtext' = 'true'), Argument('DDB1', 'quotedtext' = 'true'), Argument('KEYW', 'quotedtext' = 'true') ) ), TextBox['TB2']( 'editable' = 'false', 5..45, 'font' = Font("times", bold, 16) ) ), BoxRow('halign' = 'center', Button( "Clear", 'font' = Font("arial", bold, 15), Evaluate('function' = 'clearvar()') ), Button( "Quit", 'font' = Font("arial", bold, 15), Shutdown() ) ) ) ), Window['W2']( 'title'="ADFGX/ADFGVX Array", BoxColumn('halign' = 'none', BoxRow('halign' = 'none', Label("Enter Array: ", 'font' = Font("arial", 17)), BoxColumn('halign' = 'none', 'background' = 'navy', seq(BoxColumn('halign' = 'none', spacing = 1, inset = 1, 'background' = 'navy', BoxRow('halign' = 'none', spacing = 1, inset = 1, 'background' = 'navy', seq(TextField[cat("PT", convert((i-1)*7+j, string))]('halign' = 'center', 'width' = 2, 'font' = Font("arial", bold, 14), 'foreground' = 'black', 'background' = 'white'), j = 1..7))), i = 1..7), Button("Enter random array", 'font' = Font("arial", bold, 15), RunWindow('W3')) )), BoxRow(), BoxRow('halign' = 'none', Button("Check array", 'font' = Font("arial", bold, 15), Action(Evaluate('function' = "upmatrix", Argument('DDST', 'quotedtext' = 'true')), Evaluate('function' = "checkmat", Argument('DDST', 'quotedtext' = 'true')))), Button("Go back to ADFGX/ADFGVX window", 'font' = Font("arial", bold, 15), Action(Evaluate('function' = "upmatrix", Argument('DDST', 'quotedtext' = 'true')), CloseWindow('W2')))), BoxRow()) ), Window['W3']( 'title'="Random Array Seed", BoxColumn('halign' = 'none', BoxRow('halign' = 'none', "Enter Seed for Random Array: ", TextBox['SEED'](4..35, 'font' = Font("times", bold, 16), 'editable' = 'true')), BoxRow('halign' = 'none', Button("Go back to ADFGX/ADFGVX Array window", 'font' = Font("arial", bold, 15), Action(Evaluate('function' = "randmatrix", 'SEED', Argument('DDST', 'quotedtext' = 'true')), CloseWindow('W3')))) ) ), Action['A1'](RunWindow('W1')) ): Maplets[Display]( adfgvxmaplet ): # # # # # # # # # # # #