10
$\begingroup$

I'd like to use Mathematica 10's new Encrypt and Decrypt functions to secure files and folders on my mac.

Here's what I have so far but something goes wrong along the way, since by the end of this process the original file and the encrypted-and-then-decrypted file have different sizes:

(* Step 1: zip the target directory *)
    CreateArchive["~/input", "~/input.zip"]    
    FileByteCount@"~/input.zip"
    (*356431*)

(* Step 2: Read + encrypt + export it *)
    t = Import["~/input.zip", "Text"];
    ByteCount@t
    (*532888*)       
    Export["~/encrypted.txt", Encrypt["my password", t], "Text"];

(* Ok half way there, now for the opposite direction *)

(* Step 3: Read + decrypt it *)
    t = Decrypt["my password", 
       ToExpression@Import["~/encrypted.txt", "Text"]];

    ByteCount@t    
    (*532888 good so far*)

(* Step 4: Export and unzip it  *)    
    Export["~/output.zip", t, "Text"];
    FileByteCount /@ {"~/input.zip", "~/output.zip"}
    (*{356431, 532809} mismatch, something went wrong*)

Solution

Clear[EncryptPath, DecryptPath];
EncryptPath[path_, pass_, encryptedFileName_:"~/enc.txt"] := Module[
    {tmp = "~/enc_temp.zip"},
    CreateArchive[path, tmp];
    enc = Encrypt[pass, ToExpression @ Import[tmp, "Byte"]];
    DeleteFile[tmp];
    Export[encryptedFileName, Compress @ ToString @ InputForm @ enc]
]

DecryptPath[path_, pass_, decryptedFileName_:"~/decrypted.zip"] := Module[{enc},
    enc = ToExpression @ Uncompress @ Import[path];
    Export[decryptedFileName, Decrypt[pass, enc], "Byte"]
]
$\endgroup$
5
  • 4
    $\begingroup$ Are you sure you don't already have a self-encrypting drive? If not, I'd recommend that instead. You may also encrypt other files, but generally a hands-off approach is better. $\endgroup$
    – Moo
    Jan 15, 2016 at 19:21
  • $\begingroup$ And the problem is probably the same as in Can't inject EncryptedObject $\endgroup$
    – Kuba
    Jan 15, 2016 at 19:25
  • $\begingroup$ Do you know that you can create encrypted (size adjustable) disk images with disk utility? $\endgroup$
    – Sascha
    Jan 15, 2016 at 20:35
  • $\begingroup$ Yes I know all things things, but I'd still like to encrypt files with Mathematica. $\endgroup$
    – M.R.
    Jan 15, 2016 at 21:17
  • $\begingroup$ My hard disk is encrypted but I'd like to keep some files private from people who have my passwords. $\endgroup$
    – M.R.
    Jan 18, 2016 at 22:45

3 Answers 3

7
$\begingroup$

It is Compress\Uncompress that is throwing a wrench in things. Why do you need that anyway? I'm sure you aren't getting much compression.

This works.

Export["test.txt", Encrypt["my password", img], "Text"]
Decrypt["my password", ToExpression@Import["test.txt", "Text"]]

You don't need to export to see that the compression is an issue, just do this:

Decrypt["my password", 
 Uncompress@Compress@Encrypt["my password", img]]

EncryptedObject[] is not a ByteArray or valid EncryptedObject.

$\endgroup$
2
  • $\begingroup$ That helps. So now what about files and directories? $\endgroup$
    – M.R.
    Jan 15, 2016 at 22:48
  • $\begingroup$ Your answer only address a previous issue, but doesn't answer the question. If you can get the steps to encode and decode a local file/directory, then I can award you the points! $\endgroup$
    – M.R.
    Jan 23, 2016 at 23:26
5
$\begingroup$

If you've upgraded to wolfram 12 now, its pretty simple: To encrypt

EncryptFile["password",#,#,OverwriteTarget->True]& /@ FileNames[All,"C:\\path\\to\\directory",Infinity]

And to decrypt

DecryptFile["password",#,#,OverwriteTarget->True]& /@ FileNames[All,"C:\\path\\to\\directory",Infinity]

(remember to change "password" and "C:\path\to\directory" to whatever you'd like.)

How it works

EncryptFile works on individual files. This command maps the function to each file in the directory and overwrites the old file with the new one.

$\endgroup$
2
+50
$\begingroup$

Using the "Byte" format when importing and exporting seems to work.

In[1]:= CreateArchive["input", "input.zip"]
Out[1]= "C:\\Users\\Andy\\Documents\\input.zip"

In[2]:= enc = Encrypt["password", Import["input.zip", "Byte"]]
Out[2]= EncryptedObject[<|Data -> ByteArray[< 320 >], 
  InitializationVector -> ByteArray[< 16 >], 
  OriginalForm -> Expression|>]

In[3]:= Export["output.zip", Decrypt["password", enc], "Byte"]
Out[3]= "output.zip"

In[4]:= FileByteCount["input.zip"] == FileByteCount["output.zip"]
Out[4]= True
$\endgroup$
3
  • $\begingroup$ You're right, that's weird that it won't work with out that, hmm. $\endgroup$
    – M.R.
    Jan 25, 2016 at 0:21
  • $\begingroup$ You are missing the middle two steps, i.e. save the encrypted information to a standalone file, and then read that back in. $\endgroup$
    – M.R.
    Jan 25, 2016 at 0:31
  • $\begingroup$ I was able to use your observation to write up a solution, thanks. $\endgroup$
    – M.R.
    Jan 25, 2016 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.