Message Boards Message Boards

5
|
9207 Views
|
3 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Store encrypted binary data to disk

Posted 10 years ago

Encrypt[] and Decrypt[] perform as expected. As an exercise I wanted to write encrypted raw binary data to disk for a block based cipher. In such a case there will be an initialization vector which guards against detection of repeated messages. Encrypt[] creates this for you each time the function is called on a message. One could write the resulting EncryptedObject out to disk using an appropriate file format. However, in my case I wanted to write out only the raw data. To do this I would also need to store the initialization vector data along side the encrypted file. That makes two files per message. To decode the file the initialization vector would need to be read back in. Below is the code I used.

$fPrefix = "em";  (* em = encrypted message *)

rndFileName[] := 
 StringJoin[{$fPrefix, ToString@RandomInteger[{10000, 20000}]}]

ivExtract[encObject_EncryptedObject] := 
 ByteArray@Normal@((Association @@ encObject)["InitializationVector"])

ivWrite[outfile_, encObject_EncryptedObject] := (
  iv = Normal@ivExtract[encObject];
  BinaryWrite[outfile, iv];
  Close[outfile];
  ByteArray@iv
  )

encWrite[outfile_, encObject_EncryptedObject] := (
  BinaryWrite[outfile, Normal@Normal@encObject];
  Close[outfile])

encToFile[key_, msg_String, fname_String] := Module[{eo, iv},
  eo = Encrypt[key, msg];
  encWrite[fname, eo];
  ivWrite[fname <> ".iv", eo];
  eo
  ]

dec[key_, data_ByteArray, iv_ByteArray] := 
 Decrypt[key, 
  EncryptedObject[<|"Data" -> data, "InitializationVector" -> iv, 
      "OriginalForm" -> String|>]]

decFile[key_, fname_String] := Module[{data, dataIV},
  dataIV = ByteArray@BinaryReadList[fname <> ".iv"];
  data = ByteArray@BinaryReadList[fname];
  dec[key, data, dataIV]
  ]

Usage follows:

SetDirectory[$HomeDirectory];
key = GenerateSymmetricKey[
   Method -> <|"Cipher" -> "Blowfish", "KeySize" -> 192, 
     "BlockMode" -> "CFB"|>];
key >> "testKeySym-bf-cfb-192";  (* save the key*)
(*  key = << 
  "testKeySym-bf-cfb-192" ;   *)



message = "
  Little Secret Hoppy Ale / 55 IBU / 6% ABV
  Little Secret falls somewhere between Session IPA and American Pale \
Ale. Using a blend of three malts and four hops, Little Secret \
presents an herbal, fruity hop character punctuated by a citrus \
bitterness that lasts through the finish.";

outfile = rndFileName[];
encToFile[key, message, outfile];



In[960]:= FileNames[outfile <> "*"]

Out[960]= {"em15466", "em15466.iv"}

In[959]:= BinaryReadList[outfile][[;; 15]]

Out[959]= {73, 207, 234, 169, 245, 89, 128, 25, 232, 22, 114, 44, \
163, 242, 213}


In[958]:= decFile[key, outfile]

Out[958]= "
Little Secret Hoppy Ale / 55 IBU / 6% ABV
Little Secret falls somewhere between Session IPA and American Pale \
Ale. Using a blend of three malts and four hops, Little Secret \
presents an herbal, fruity hop character punctuated by a citrus \
bitterness that lasts through the finish."
POSTED BY: Philip Dutton
3 Replies
Posted 10 years ago

OK, that works. I had originally tried using Export to write the EncryptedObject rather than OpenWrite/Write/Close. Thanks.

POSTED BY: David G

To write a list of EncryptedObject out to disk and then read it back in try this approach:

In[972]:= $fPrefix = "encrypted-messages-"; 

In[973]:= rndFileName[] := 
 StringJoin[{$fPrefix, ToString@RandomInteger[{10000, 20000}]}]

In[974]:= SetDirectory[$HomeDirectory];

In[975]:= 
key = GenerateSymmetricKey[
   Method -> <|"Cipher" -> "Blowfish", "KeySize" -> 192, 
     "BlockMode" -> "CFB"|>];

In[993]:= message = "
    Little Secret Hoppy Ale / 55 IBU / 6% ABV
    Little Secret falls somewhere between Session IPA and American \
Pale Ale. Using a blend of three malts and four hops, Little Secret \
presents an herbal, fruity hop character punctuated by a citrus \
bitterness that lasts through the finish.";

In[992]:= encrypted = Encrypt[key, message];

In[1004]:= messages = 
 Table[encrypted, {i, 1, 
   25}];     (* list of encrypted messages (all the same in this \
example)*)

In[1005]:= fname = rndFileName[]

Out[1005]= "encrypted-messages-12078"

In[1006]:= out = OpenWrite[fname];

In[1007]:= Write[out, messages]

In[1008]:= Close[out]

Out[1008]= "encrypted-messages-12078"

In[1012]:= Head[First@Flatten@ReadList[fname]]

Out[1012]= EncryptedObject
POSTED BY: Philip Dutton
Posted 10 years ago

One could write the resulting EncryptedObject out to disk using an appropriate file format.

Can you expand on this statement? I don't know of a way to write the EncryptedObject to a file. The documentation falls short in this area. I would also like to be able to read the file and Decrypt the EncryptedObject. The only method I've worked out was similar to yours. Only, I write the Data and InitializationVector as a pair of lists of character codes to a single text file.

POSTED BY: David G
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