Converting a md5 hash byte array to a string

前端 未结 7 1542
余生分开走
余生分开走 2021-01-31 13:28

How can I convert the hashed result, which is a byte array, to a string?

byte[] bytePassword = Encoding.UTF8.GetBytes(password);

using (MD5 md5 = MD5.Create())
         


        
7条回答
  •  走了就别回头了
    2021-01-31 13:55

    Well, you could use the string constructor that takes bytes and an encoding, but you'll likely get a difficult to manage string out of that since it could contain lots of fun characters (null bytes, newlines, control chars, etc)

    The best way to do this would be to encode it with base 64 to get a nice string that's easy to work with:

    string s = Convert.ToBase64String(bytes);
    

    And to go from that string back to a byte array:

    byte[] bytes = Convert.FromBase64String(s);
    

提交回复
热议问题