English

How To: Encrypt and decrypt a script document using Avenue

Summary

The first script in this article uses a character replacement scheme to write an encrypted version of a script document to a text file. The second script reverses the encryption to recreate the original script.

Procedure

  1. Open a new script window.

    A. Activate the Project window.
    B. Click the Scripts icon.
    C. Click New.

  2. Rename the script "Encrypt."

    A. Select Properties from the Script menu.
    B. Type in a new name in the Name field.
    C. Click OK.

  3. Copy the following code into the new window:

    Code:
    '-- Encrypt TextFile
    '-- Back up scripts before encrypting. Use the script key
    '-- to unencrypt the scripts.
    '-- Back up this script, change password and encrypt this script.

    theScript = av.GetActiveDoc
    theDelete = theScript
    theFName = av.GetProject.MakeFileName("scrpt", "ave")

    theFName = FileDialog.Put( theFName, "*", "Write Script")
    if (nil = theFName) then
    exit
    end

    f = LineFile.Make(theFName, #FILE_PERM_WRITE)
    if (f = nil) then
    msgbox.error("Cannot open file:"++theFName.GetName, "")
    exit
    end

    if (theScript.GetSelected.Count = 0) then
    '-- Change the order of the second parameter in
    '-- translate to set your own key. Use all 26 letters
    '-- only once in random order, then do the same to key
    theKey = theScript.GetSource.translate(
    "abcdefghijklmnopqrstuvwxyz",
    "zyxwvutsrqponmlkjihgfedcba")
    theScript = theKey
    f.WriteElt(theScript)
    av.ShowMsg( "Script written to"++theFName.GetFullName )
    else
    f.WriteElt(theScript.GetSelected)
    av.ShowMsg( "Selected text written to"++theFName.GetFullName )
    end

    f.close
    if (System.GetOS = #SYSTEM_OS_MAC) then
    Script.Make("MacClass.SetDocInfo(SELF, SEd)").DoIt(theFName)
    end
    av.GetProject.RemoveDoc(thedelete)

  4. Open another new script window.
  5. Rename the script "Unencrypt."
  6. Copy the following code into the new script window:

    Code:
    '-- Encrypt TextFile Key
    '-- Back up this script, change password and encrypt this script.

    theTest = MsgBox.Password
    thePass = "esri"
    if (theTest = thePass) then
    theSEd = av.GetActiveDoc
    file_names = FileDialog.ReturnFiles({"*.ave", "*"},
    {"Avenue Script", "Text file"}, "Load Script", 0)
    if (file_names.count < 1) then exit end
    IsEncrypt = MsgBox.YesNo("Is Script Encrypted?", "Encrypted?", TRUE)
    if (isEncrypt = TRUE) then
    for each x in file_names
    f = TextFile.Make(x, #FILE_PERM_READ)
    t = f.read(f.GetSize)
    v = t.translate("zyxwvutsrqponmlkjihgfedcba",
    "abcdefghijklmnopqrstuvwxyz")
    theSEd.Insert(v)
    f.Close
    end
    else end
    else MsgBox.Info("PassWord is Incorrect", "Info")
    end

  7. Attach each script to a new button on the Script GUI.

    A. Compile the script.
    B. Switch to the Project window.
    C. Select Customize from the Project menu.
    D. On the Customize dialog, select Script under Type dropdown.
    E. Select Buttons under Category.
    F. Click the New button.
    G. Double-click the Click property in the Customize dialog box.
    H. Enter the name of the script in the Script Manager and click Select.
    I. Close the Customize dialog box.

    For more information, see 'Customize dialog box' in ArcView Help.

  8. Open the script document you wish to encrypt and click the new Encrypt button.

    Note:
    You must unencrypt the script in order to use it in ArcView. To unencrypt the script click the Unencrypt button and select the file.