1

Dears

I have a byte array that is returned from web server , it is a part of json-serialized object (property value) It looks like below in the json string:

,"n":"y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z-tzPbtHyFVYYU2AC4SVrwQp9-ELz-..._xW3bmMxuwoBgHpWDTw"

Please note that there is no double equal sign at the end, like for Base64 strings. I've used three dots (...) to make string representation a little bit shorter
I can deserialize object and get proper byte array:

var kb = JsonConvert.DeserializeObject<KeyBundle>(Properties.Resources.keyBundleJson);

And can it serialize to json back:

JsonSerializerSettings settings = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.None,
    Formatting = Formatting.Indented
};

string json = JsonConvert.SerializeObject(kb, settings);

But the problem is that result property value looks not the same as original string:

from web server it was:
y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z-tzPbtHyFVYYU2AC4SVrwQp9-ELz-..._xW3bmMxuwoBgHpWDTw

serialized locally:
y1GpP7FibyTYl40Jhx1B90WOi1mecJfpi4IEhbHPbAB64jhV16UlpEPyGpNIzDS4Lct80sIs7FW5Vnf38Z+tzPbtHyFVYYU2AC4SVrwQp9+ELz+.../xW3bmMxuwoBgHpWDTw==

underscores and slashes, plus and minus signs, two equal signs at the end

is it possible to serialize byte array exactly as it is done by web-server? I have an idea to serialize it with Json and then replace minus with plus, underscore with slash and remove last two equal signs.

Any other method to get it immediately out of the box?

Regards

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • It looks like [modified base64](https://stackoverflow.com/questions/1228701/code-for-decoding-encoding-a-modified-base64-url). – ProgrammingLlama May 08 '18 at 10:06
  • There are many different base64 variants. Look at this: https://en.wikipedia.org/wiki/Base64#Variants_summary_table – arekzyla May 08 '18 at 10:06
  • @arekzyla: argh you beat me to it. oleksa: However, there does not seem to be a way to specify the variant in the C# implementation. You can only tell the method to format the string with line breaks after every 76 characters. It seems you will have to make your own implemtation to get the right variant. You probably have the RFC7515 variant from the web-server and get RFC4648 in C#. Stefan Zvonar's answer in the post John linked will most likely be exactly what you need to do. – Heki May 08 '18 at 10:20

1 Answers1

1

In urls there is different variant of Base64 used with - and _ which doesn't require additional encoding (e.g. + would be encoded to %2B). For this you can simply use string Replace method to replace those characters.

If you want an out-of-the box solution you can try Microsoft.IdentityModel.Tokens nuget package:

var encoded = Base64UrlEncoder.Encode(someString);
var decoded = Base64UrlEncoder.Decode(encoded);

For more info: https://en.wikipedia.org/wiki/Base64#URL_applications

arekzyla
  • 2,878
  • 11
  • 19