Message Boards Message Boards

2 Replies
5 Total Likes
View groups...
Share this post:

How Can I Password-Protect a Mathematica Notebook?

Posted 10 years ago

Hello everyone,

How can I password-protect a Mathematica notebook?


POSTED BY: Gregory Lypny
2 Replies
Posted 10 years ago

Thanks David,

Much appreciated.


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 = 
      ToString[RandomInteger[{100000, 999999}]] <> ".m"}];
   Export[source, theNB, "Package"];
   Encode[source, file, password];

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

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

Group Abstract Group Abstract