10

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"]
]
M.R.
  • 31,425
  • 8
  • 90
  • 281

3 Answers3

7

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.

george2079
  • 38,913
  • 1
  • 43
  • 110
  • That helps. So now what about files and directories? – M.R. Jan 15 '16 at 22:48
  • 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! – M.R. Jan 23 '16 at 23:26
5

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.

ions me
  • 881
  • 5
  • 11
2

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
Andy C.
  • 623
  • 3
  • 9
  • You're right, that's weird that it won't work with out that, hmm. – M.R. Jan 25 '16 at 00:21
  • You are missing the middle two steps, i.e. save the encrypted information to a standalone file, and then read that back in. – M.R. Jan 25 '16 at 00:31
  • I was able to use your observation to write up a solution, thanks. – M.R. Jan 25 '16 at 00:56