Skip to main content

export - Inconsistent behavior of undocumented Encode->Import->StringToStream->Get on password-locked stream


Let's save a definition. Encode it with password/key and Get it again.


We will not use Get directly on directory but with combination of Get+StringToStream+Import.


Get works with streams since V9.0 so I see no reason not to go this way.


file = FileNameJoin[{$TemporaryDirectory, "def.m"}];
fileEnc = FileNameJoin[{$TemporaryDirectory, "def.enc"}];
DeleteFile /@ {file, fileEnc} //Quiet; (*just in case*)


ClearAll @ f;
f[x_] := x^2;
Save[file, f];

password = "key";
Encode[file, fileEnc, password];

ClearAll @ f;
stream = StringToStream @ Import[fileEnc, "Text"];

Get[stream , password];
Close[stream];
f[2]


4



Yeah, great... fortunately I've tested this with different password:


password = "kuba";


Encode[file, fileEnc, password];

ClearAll @ f;
stream = StringToStream @ Import[fileEnc, "Text"];
Get[stream , password];
Close[stream];
f[2]


Syntax::sntx: Invalid syntax in or before "f[x_] :eeev" (line 1 of "String["(*!1N!*)4mx.

^ w24yf0¡'h;1;U.#+"]")
f[2]



From my observations it seems to be quite random.


p.s. using Get directly will work. but this is not what I'm after.


Reproduced on V9 V10 Win7



Answer



Being able to use this provides us with a way to produce quite secure CDFs designed for FreePlayer.





Maybe it is a problem with encoding somewhere or with overloaded definitions of Get.


Nevermind, as pointed out by Rolf Mertig if one uses OpenRead with DefineInputStreamMethod to convert a binary file to a stream, everything seems to work.




Steps:



  • DefineInputStreamMethod["ByteList", ...

  • binarydata = Import[encodedFile, "Binary"]

  • stream = OpenRead["whateverName", Method -> {"ByteList", "Bytes" -> binarydata}]

  • Get[stream, password]





Execution:


(* This part is taken bit by bit from the documentation 
of DefineInputStreamMethod *)

DefineInputStreamMethod["ByteList", {
"ConstructorFunction" ->
Function[{name, caller, opts},
If[MatchQ[opts, {___, "Bytes" -> {_Integer ...}, ___}],
{True, {0, "Bytes" /. opts}},

{False, $Failed}
]],

"ReadFunction" ->
Function[{state, n},
Module[{pos = state[[1]], bytes = state[[2]], bytesRead},
If[pos >= Length[bytes],
{{}, state},
bytesRead = Part[bytes, pos + 1 ;; Min[Length[bytes], pos + n]];
{bytesRead, {pos + Length[bytesRead], bytes}}

]
]],

"EndOfFileQFunction" -> ({#[[1]] >= Length[#[[2]]], #} &)
}]



file = FileNameJoin[{$TemporaryDirectory, "def.m"}];
fileEnc = FileNameJoin[{$TemporaryDirectory, "def.enc"}];
DeleteFile /@ {file, fileEnc} // Quiet;(*just in case*)

ClearAll@f;

f[x_] := x^2;
Save[file, f];

password = "kuba";
Encode[file, fileEnc, password];

ClearAll@f;


bin = Import[fileEnc, "Binary"];

stream = OpenRead["dumpsave", Method -> {"ByteList", "Bytes" -> bin}];

Get[stream, password];

Close[stream];

f[2]



4

Comments