1

Not sure how to ask this question because I may not have the concepts down. I'm also not a C# or .Net person at all.

For a test app, cookie value is base64 wrapped binary data that is using BinaryFormatter for .Net. I'm trying to figure out how to replicate this in python so that I can deserialize, modify data and serialize to finally be sent via HTTP cookie header. I can manage the HTTP request. It looks like a dictionary of value that is then converted to a BitArray. I've looked at pickle, marshal, BitArray and so on. Lots of examples out there but, nothing close to what I'm trying do to AFAIK.

But again, I barely know how to ask the question. Here's an example of the data

AAEAAAD/////AQAAAAAAAAAEAQAAAOIBU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuRGljdGlvbmFyeWAyW1tTeXN0ZW0uU3RyaW5nLCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldLFtTeXN0ZW0uT2JqZWN0LCBtc2NvcmxpYiwgVmVyc2lvbj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODldXQMAAAAHVmVyc2lvbghDb21wYXJlcghIYXNoU2l6ZQADAAgWU3lzdGVtLk9yZGluYWxDb21wYXJlcggAAAAACQIAAAAAAAAABAIAAAAWU3lzdGVtLk9yZGluYWxDb21wYXJlcgEAAAALX2lnbm9yZUNhc2UAAQEL

gitsitgo
  • 6,589
  • 3
  • 33
  • 45
Rayvin
  • 11
  • 4
  • Possible duplicate of [C# - struct serialization](http://stackoverflow.com/questions/28915076/c-sharp-struct-serialization) – ivan_pozdeev Apr 11 '17 at 19:48
  • That one has a code sample for IronPython and reference links to MSDN for algorithms. – ivan_pozdeev Apr 11 '17 at 19:48
  • I'm not familiar with C#/.Net and I don't have windows. I've read through the .Net library stuff on BinaryFormatter but it doesn't say AFAIK who it constructs that object and then coverts it to binary. But again, not knowing enough about this I could be approaching it wrong. – Rayvin Apr 11 '17 at 20:03

1 Answers1

0

This is BinaryFormatter's serialization format. It is documented in MSDN as per .Net Where to find the official specification of the BinaryFormatter serialization format?. It basically consists of the object's full .NET type name (together with the assembly's Strong Name) and the contents of all its fields (which may include any number of other, similarly represented, objects).

So, to serialize/deserialize, you need:

  • A parser/generator for the format
  • Relevant bits of the exact versions of .NET types being (de)serialized - something that can produce/process the exact same field data

So, you either use the corresponding logic from a .NET implementation, or make an alternative implementation of both relevant BinaryFormatter subroutines and the types (field data adapters for the types) (to be) present in the serialized data.


That said, a "native" serialization format is not designed to be an exchange format that can be read anywhere, so it's a poor choice for a cookie. That's because its purpose is rather to be able to restore the exact same object, so it's inherently bound to the specific internal structure of a specific implementation of a specific version of a type (down to architecture-dependent details).

Instead, even .NET standard library has a whole number of other formats that are more fit for data exchange.

Community
  • 1
  • 1
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • So, download the electronic book on ASP.Net programming, stop whining and start writing is what you're saying? Thanks all for the help. At least I can check a few things off the my list. Cheers! – Rayvin Apr 11 '17 at 21:40
  • Even before that, I'd try and convince the service's team to change the format to something less problematic. By problematic, I mean not only for you (and their other clients) but for themselves, too. E.g. things will break each time the version of any serialized object's assembly changes, and for any server/client that has the version other than their server's. – ivan_pozdeev Apr 12 '17 at 02:11
  • @Rayvin well, http://stackoverflow.com/a/28915300/648265 has a usage example for IronPython. It should be the same for Python.NET minus the interface used to access .NET facilities. – ivan_pozdeev Apr 21 '17 at 22:51