' ' Visualize the difference between plaintext and encrypted values ' as a function of position and character. ' ' Requires: String.AsEncrypted, a script to encrypt arbitrary ASCII strings. ' Requires: LineFile.makeTemp, a script to create a temporary linefile (and its name). ' strTitle = "Visualize encryption" nLength = 100 ' Length of plaintext strings to encrypt (= number of columns in visualization) nStart = 32 ' Start ASCII value (= bottom row in visualization) nStop = 127 ' Ending ASCII value (= top row in visualization) strNoData = "-999"' Used in ASCII grid representation for missing values ' ' Create output files for the plaintext and encrypted text. ' lstPlain = av.Run("LineFile.MakeTemp", NIL) lstCrypt = av.Run("LineFile.MakeTemp", NIL) LFPlain = lstPlain.Get(1) LFCrypt = lstCrypt.Get(1) if ((LFPlain = NIL) or (LFCrypt = NIL)) then return NIL end FNPlain = lstPlain.Get(0) FNCrypt = lstCrypt.Get(0) ' ' (Grid construction technique: create an ASCII export file, then import it later.) ' Write the header records for the ASCII grid representation. ' These formulas will CENTER the cells at integral coordinates. ' sHeader = "ncols " + nLength.SetFormatPrecision(0).AsString + NL + "nrows " + (nStop-nStart+1).SetFormatPrecision(0).AsString + NL + "xllcorner -0.5" + NL + "yllcorner " + (nStart-0.5).SetFormatPrecision(1).AsString + NL + "cellsize 1" + NL + "nodata_value " + strNoData LFPlain.WriteElt(sHeader) LFCrypt.WriteElt(sHeader) ' ' Loop over all ASCII characters from nStart through nStop. ' Go backwards, since ArcView reads grids from top row to bottom row. ' av.ShowStopButton av.SetStatus(0) strErr = "" for each i in (nStop max nStart)..(nStop min nStart) by -1 doMore = av.SetStatus((nStop max nStart-i+0.5) * 100 / ((nStop-nStart).Abs + 1)) if (not doMore) then return NIL end ' ' Create a string of ASCII value i, length nLength, ' and encrypt it. ' c = i.AsChar s = String.MakeBuffer(nLength).Substitute(" ", c) e = av.Run("String.AsEncrypted", s) ' ' Test the expected one-for-one transliteration. ' if (e.Count <> s.Count) then strErr = strErr + "Plaintext for" ++ c ++ " (" + i.AsString + ") has" ++ s.Count.AsString ++ "characters while encrypted text has" ++ e.Count.AsString + NL end ' ' Output the plaintext (s) and the encrypted text (e) as ' a series of ASCII values of their characters. ' n = nLength strA = "" strB = "" while (s.IsNull.Not and e.IsNull.Not) a = (s.Left(1).AsASCII + 256) mod 256 ' On Windows machines at least, AsASCII can be negative! b = (e.Left(1).AsASCII + 256) mod 256 s = s.Right(s.Count-1) e = e.Right(e.Count-1) n = n-1 strA = strA ++ a.SetFormatPrecision(0).AsString strB = strB ++ b.SetFormatPrecision(0).AsString end if (n > 0) then ' Truncated lengths occur when \0x00 occurs in the encrypted string strOut = String.MakeBuffer(nLength).Substitute(" ", strNoData + " ") end LFPlain.WriteElt(strA) LFCrypt.WriteElt(strB) end if (strErr.IsNull.Not) then MsgBox.Report(strErr, strTitle) end LFPlain.Close LFCrypt.Close ' ' (One could delete the temporary files, but we leave them around for ' further processing if desired.) ' ' ' Create the grids. ' useFloat = false lstFN = {FNPlain, FNCrypt} lstGrids = {} for each theFN in lstFN lstGrids.Add(Grid.MakeFromASCII(theFN, useFloat)) end lstGrids.Add( ( lstGrids.Get(1) - lstGrids.Get(0) ) + 128 % 256 - 128 ) ' ' Obtain a view to show the results. ' theView = av.GetActiveDoc if (theView.Is(View).Not) then theView = av.FindDoc("View1") end if (theView = NIL) then theView = View.Make end ' ' Visualize the grids. ' lstNames = {"Plaintext", "Encrypted text", "Residual"} for each i in 0..2 G = lstGrids.Get(i) if (G.HasError) then MsgBox.Error("Cannot create the " + lstNames.Get(i) + " theme.", strTitle) else theTheme = GTheme.Make(G) theTheme.SetName(lstNames.Get(i)) theView.AddTheme(theTheme) end end ' end of script