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

时间:2016-01-07 15:22:10

标签: 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 , 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#

    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 ;




     }
}

0 个答案:

没有答案