Message Boards Message Boards

1
|
7626 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How Can I Password-Protect a Mathematica Notebook?

Posted 11 years ago

Hello everyone,

How can I password-protect a Mathematica notebook?

Gregory

POSTED BY: Gregory Lypny
2 Replies
Posted 11 years ago

Thanks David,

Much appreciated.

Gregory

POSTED BY: Gregory Lypny

One approach is to compress the notebook into a compressed password-protected Zip file. Of course once unzipped it would be available to anyone who wished to share it, but this will be true no matter how you protect your notebook since anyone who has it can copy and paste the contents...

Here is another approach using Encode as the basis. it can be extended in a variety of ways, but this is a simple approach if you want to share notebooks entirely from within Mathematica but to protect them.

Use the following code:

passwordEncodeNotebook[nb_NotebookObject, password_String] :=
 Catch@Module[{theNB, file, source},
   theNB = NotebookGet[nb];
   file = 
    SystemDialogInput["FileSave", {"FileName", {"*.m" -> {"*.m"}}}, 
     WindowTitle -> "Export Filename"];
   If[file === $Canceled, Throw[$Canceled]];

   source = 
    FileNameJoin[{DirectoryName[file], 
      ToString[RandomInteger[{100000, 999999}]] <> ".m"}];
   Export[source, theNB, "Package"];
   Encode[source, file, password];
   DeleteFile[source];
   file
   ]

Then, using this function, you can save a given notebook as a password protected .m file. So, if a notebook's NotebookObject is nb, then you would create the protected.m file (using "test" as the password)--the function asks for a file name to save it as:

passwordEncodeNotebook[Enb, "test"]

To recover the notebook you would then use

NotebookPut@Get["path to the file", "test"]

One way to extend this approach is to actually embed the encoded .m file in the original Notebook's TaggingRules option and to then include the method to retrieve the encoded content in an interactive InputField and Button in the original notebook. This is the approach taken in the notebook password protection functionality in http://scientificarts.com/worklife/

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract