0

I tried to convert this C# code (encoding and serialization) to Python but results are different. Why?

    var dictt = new Dictionary<string, object>
    {
        { "aaa", "6mjDx3Cya4JvbTLMenPpXA==" },
        { "bbb", "4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="},
    };

    JavaScriptSerializer serialzr = new JavaScriptSerializer();
                       
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(serialzr.Serialize(dictt))); //eyJpdiI6IjZtakR4M0N5YTRKdmJUTE1lblBwWEE9PSIsInZhbHVlIjoiNFU1TStWMnlvSUE3cldqNDZyZGhUQmdwRWpmMXpZSzBtMTFsRE03RFJDST0ifQ==
dictt = {
    "aaa": "6mjDx3Cya4JvbTLMenPpXA==",
    "bbb": "4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="
    }
     
    y = json.dumps(dictt)
    #y= {"aaa": "6mjDx3Cya4JvbTLMenPpXA==", "bbb": "4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="}
    json_object = json.loads(y) #convert it to json, like serialize in C#
    # json_object = {'aaa': '6mjDx3Cya4JvbTLMenPpXA==', 'bbb': '4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI='} 
    json_object_utf8_encoded = str(json_object).encode('utf8') #encode utf8
    #json_object_utf8_encoded = b"{'aaa': '6mjDx3Cya4JvbTLMenPpXA==', 'bbb': '4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI='}"
    json_base64 = base64.b64encode(json_object_utf8_encoded) #convert to base64 string      
    #json_base64 = "b'eydpdic6ICc2bWpEeDNDeWE0SnZiVExNZW5QcFhBPT0nLCAndmFsdWUnOiAnNFU1TStWMnlvSUE3cldqNDZyZGhUQmdwRWpmMXpZSzBtMTFsRE03RFJDST0nfQ=='"
    json_base64_str = json_base64.decode("utf-8") 
    return json_base64_str 
    #eydpdic6ICc2bWpEeDNDeWE0SnZiVExNZW5QcFhBPT0nLCAndmFsdWUnOiAnNFU1TStWMnlvSUE3cldqNDZyZGhUQmdwRWpmMXpZSzBtMTFsRE03RFJDST0nfQ==
2
  • Might be easier if you just looked the non-base64 form to see the difference Commented Jan 13, 2021 at 7:49
  • I added all values in comments @SamiKuhmonen Commented Jan 13, 2021 at 8:41

1 Answer 1

2

There's a small difference between the following two python commands, resulting with two different outputs: " vs. '.

While this one:

serialized = json.dumps(dict)
print(serialized)

outputs:

{"aaa": "6mjDx3Cya4JvbTLMenPpXA==", "bbb": "4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="}

This one:

serialized = json.dumps(dict)
deserialized = json.loads(serialized)
print(str(deserialized))

outputs:

{'aaa': '6mjDx3Cya4JvbTLMenPpXA==', 'bbb': '4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI='}

You work with the latter one: you're unnecessarily deserializing the json, and then calling str upon it, giving you the different result. Just drop the json.loads step.

EDIT:

The returned values (C# vs python) will still not match, because C# serializer (whether it's JavaScriptSerializer or the more common JsonConvert.Serialize of Newtonsoft) outputs a json with no spaces:

{"aaa":"6mjDx3Cya4JvbTLMenPpXA==","bbb":"4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="}

while python's json.dumps outputs a json with spaces:

{"aaa": "6mjDx3Cya4JvbTLMenPpXA==", "bbb": "4U5M+V2yoIA7rWj46rdhTBgpEjf1zYK0m11lDM7DRCI="}

Even if you'd pass an indentation argument to json.dumps (e.g., json.dumps(dict, indent=2)), you can't do that with JavaScriptSerializer, and although you could do it with Newtonsoft's JsonConvert.Serialize it would still not work, since the latter uses CRLF characters for the indentation, while python doesn't.

That being said, it shouldn't matter, because the logical output is the same: A Base64 encoding of the serialized dictionary.

Sign up to request clarification or add additional context in comments.

3 Comments

I removed json.loads step and str convertion in json_object_utf8_encoded = str(json_object).encode('utf8') line. But new result is eyJpdiI6ICI2bWpEeDNDeWE0SnZiVExNZW5QcFhBPT0iLCAidmFsdWUiOiAiNFU1TStWMnlvSUE3cldqNDZyZGhUQmdwRWpmMXpZSzBtMTFsRE03RFJDST0ifQ== . It's still not same.
I want literally same results because after I generate python result, I will send this result as parameter to c# method and c# code will decripte it. ( and I can't change c# method. ) Is there any way of that?
"And c# code will decrypt it" - what do you mean by "decrypt"? If by that you mean that it will decode it from Base64 and then deserialize it to dictionary, then it will work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.