Message Boards Message Boards

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

Store encrypted binary data to disk

Posted 9 years ago
POSTED BY: Philip Dutton
3 Replies

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 9 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
Posted 9 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
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