在C#和Python中生成base64编码的md5

在C#和Python中生成base64编码的md5,c#,python,C#,Python,我试图将python代码转换为C语言,而转换时,我只使用了两种语言中的一个简单字符串,例如“abc”,并生成相同的编码字符串,这部分实现了我的目标,但我仍然坚持使用json对象。这里是python代码 Python import base64 , hashlib , hmac , json , httplib , socket , re , email,ssl,os from time import strptime , mktime , timezone , time , gmtime , s

我试图将python代码转换为C语言,而转换时,我只使用了两种语言中的一个简单字符串,例如“abc”,并生成相同的编码字符串,这部分实现了我的目标,但我仍然坚持使用json对象。这里是python代码

Python

import base64 , hashlib , hmac , json , httplib , socket , re , email,ssl,os
from time import strptime , mktime , timezone , time , gmtime , strftime , daylight



h=hashlib.new ('md5')
h.update(json.dumps({"username":"sym","password":"123"}))
pprint.pprint(h.digest())

pprint.pprint(base64 . b64encode(h.digest()))
C#


你说你已经部分解决了问题。你还没有完成哪一部分?什么不起作用?请解释一下,这让很多可能会回答问题的人的生活变得更轻松。我在两种语言中仅使用一个简单的字符串时生成了相同的base64编码字符串。现在解释一下,谢谢你指出这一点,因为你的实际问题与此无关,C#实际上与MCVE无关,会分散MCVE的注意力。您真正需要知道的是如何在Python中将字符串序列化为JSON?好的,您告诉我们什么是有效的。。。那么什么不起作用呢?字典是无序的,因此如果有人在不同的系统上或使用不同的语言将它们序列化为json,人们不会期望它们的md5匹配。我是否在C#中正确地转换了python代码?您说您已经部分实现了解决方案。你还没有完成哪一部分?什么不起作用?请解释一下,这让很多可能会回答问题的人的生活变得更轻松。我在两种语言中仅使用一个简单的字符串时生成了相同的base64编码字符串。现在解释一下,谢谢你指出这一点,因为你的实际问题与此无关,C#实际上与MCVE无关,会分散MCVE的注意力。您真正需要知道的是如何在Python中将字符串序列化为JSON?好的,您告诉我们什么是有效的。。。那么什么不起作用呢?字典是无序的,因此如果有人在不同的系统上或使用不同的语言将它们序列化为json,人们不会期望它们的md5匹配??
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Net.Security;
    using System.Security.Cryptography;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;



 public partial class Main : Form
    {


    static string GetMd5Hash(MD5 md5Hash, string input)
        {

        Credentials cred = new Credentials();
                cred.username = "sym";
                cred.password = "123";
                string output = JsonConvert.SerializeObject(cred);
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(output));
 string s  = Convert.ToBase64String(data);
return s ;




     }
}