Convert GUID back to string C#

PARTH DESAI 61 Reputation points
2021-04-03T17:06:21.167+00:00

Hello,

Using below code I am converting string to GUID.
I want to convert back my GUID to string using C#.

static void Main(string[] args)
        {
            string messageId = "RunBatch";
            string supplierId = "HST";
            string deviceTypeId = "Mixer";

            //Convert to GUID
            Guid guid = StringToGUID(messageId + supplierId + deviceTypeId);
            Console.WriteLine(guid.ToString());
            Console.ReadLine();

        }

        static Guid StringToGUID(string value)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(value));
            return new Guid(data);
        }
Developer technologies C#
{count} votes

3 answers

Sort by: Most helpful
  1. Davin Mickelson 121 Reputation points
    2021-04-04T05:29:39.947+00:00

    Hash algorithms are designed to run one-way (data to hash). This is why we often stored hashed passwords in a database and not encrypted passwords (which can be decrypted). It's just about impossible for a hacker to convert a hashed password back to the original password.

    Rewrite your function to not use a hash algorithm.

    1 person found this answer helpful.

  2. Viorel 122.6K Reputation points
    2021-04-03T20:14:04.63+00:00

    Because different strings could give the same hash and Guid, the reverse transformation seems problematic.

    0 comments No comments

  3. Tony Hill 1 Reputation point
    2021-04-11T10:42:21.763+00:00

    To convert from string to Guid use

    if (Guid.TryParse(stringGuid, out var newGuid))
    ...

    to create a string form a Guid just

    use the ToString method

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.