# Can't inject EncryptedObject

Bug present from 10.0 to 10.3.1 and fixed in 10.4.0

!!

Here are minimal examples:

encryptedObj = Encrypt["pass", "TestCase"];

Decrypt["pass", encryptedObj] (*returns correct "TestCase"*)

With[{x = encryptedObj}, Button["Try with", Decrypt["pass", x]]]

Button["Try with", Decrypt["pass", #]] &@encryptedObj


Clicking each button generates an error:

"data length" displayed in message for encryptedObject seems to be related to the problem because correct one is 16 bytes.

I have no idea... I tried many things e.g. Normal/different forms of expression/writing to different links. With no luck.

As Michael Hale has pointed out Module will work but I need something persisting through sessions, like:

DynamicModule[{
x = encryptedObj
},

Button["Try with", Decrypt["pass", x]]
]


which is not working either.

So yes, that's great an encryption is there, but what's the point if I can't distribute encrypted objects -.-

• If you copy the EncryptedObject from the box form (Show Expression) or from the FullForm and execute it, you get your error, invalid EncryptedObject. (I have no fix for that.) Apr 24 '15 at 10:00

I think the problem might be related to a bug in FullForm when applied to a ByteArray object:

ByteArray["aV+jpGtfd3BHhoSvOthJpQ=="] // FullForm
(* List[105,95,163,164,107,95,119,112,71,134,132,175,58,216,73,165] *)


The full form has lost information regarding the structure of the ByteArray. The box-form of the button is using this list form but the EncryptedObject operations only work with byte arrays, not lists.

Analysis

First, let's consider the BoxForm of the generated button:

With[{x = encryptedObj}
, MakeBoxes[Button["Try with", Decrypt["pass", x]]] // InputForm
]

(*
ButtonBox["\"Try with\"", RuleDelayed[ButtonFunction,
Decrypt["pass", EncryptedObject[Association[Rule["Data",
List[105, 95, 163, 164, 107, 95, 119, 112, 71, 134, 132, 175, 58,
216, 73, 165]], Rule["InitializationVector",
List[215, 104, 218, 122, 197, 88, 212, 206, 35, 98, 253, 85, 102, 27,
229, 8]], Rule["OriginalForm", String]]]]],
Rule[Appearance, Automatic], Rule[Evaluator, Automatic],
Rule[Method, "Preemptive"]]
*)


Take particular note of the value of the "Data" property of the EncryptedObject above. It is a simple list. Contrast this to the value of that property when we retrieve it directly:

encryptedObj["Data"]


It is a ByteArray, not a list. The box-form of the button has been corrupted somehow. The box form is not using the InputForm of the byte array, which looks like this:

encryptedObj["Data"] // InputForm
(* ByteArray["aV+jpGtfd3BHhoSvOthJpQ=="] *)


Rather, it appears to be using the FullForm:

encryptedObj["Data"] // FullForm
(* List[105,95,163,164,107,95,119,112,71,134,132,175,58,216,73,165] *)


I suggest that it is a bug for the FullForm of a ByteArray to be a list instead of preserving its structure. Alternatively, the code that generates the box-form of a Button should be using InputForm instead of FullForm. Then again, if lists and byte arrays are supposed to be interchangeable, then perhaps the operations on EncryptedObject should not fail when passed lists. Any way you slice it, the behaviour appears to be due to a bug.

Work-around

As suggested by @SimonWoods in a comment, a work-around is to explicitly fix the encrypted object's representation by converting the lists back into byte arrays:

fix[x_] := x /. l_List :> ByteArray[l]

With[{x = encryptedObj}
, Button["Try with", Print @ Decrypt["pass", fix[x]]]
]

• I was about to post something similar. A workaround is to convert the lists back to byte arrays x /. l_List :> ByteArray[l] Apr 24 '15 at 17:33
• I added a workaround section, with thanks to @SimonWoods. Nov 12 '15 at 15:40

OK, now I have another suggestion:

With[{x = ToString[encryptedObj, InputForm]},
Print[x];
Button["Try with", foo = Decrypt["pass", ToExpression[x]]]]


The button generates no error and foo is set to "TestCase".

• @Kuba Maybe the new suggestion works? (Still seems worth reporting your problem.) -- Sorry about the Print[x], if it's annoying. I meant to delete it. Apr 24 '15 at 10:18
• Funny but works :) Thanks. Yes, will report this.
– Kuba
Apr 24 '15 at 10:23

I think you've found a bug. It seems to me that the Encrypt/Decrypt functionality introduced in 10.1 needs more work. But, I have found that the following work around may help. If you explicitly pass the EncryptedObject properties into an EncryptedObject via its Association parameter, they will be correctly interpreted with no errors. Try this:

encryptedObj = Encrypt["pass", "TestCase"];

Dynamic[data]
DynamicModule[{},
Button["Try with", data = Decrypt["pass",
EncryptedObject[<|"Data" -> encryptedObj["Data"],
"InitializationVector" -> encryptedObj["InitializationVector"],
"OriginalForm" -> encryptedObj["OriginalForm"]|>]]]
]


The result is: