Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing a md5 hash algorithm in C# [duplicate]

Tags:

c#

Possible Duplicate:
Reversing an MD5 Hash

Given this method in c#

public string CalculateFileHash(string filePaths) {
    var csp = new MD5CryptoServiceProvider();
    var pathBytes = csp.ComputeHash(Encoding.UTF8.GetBytes(filePaths));
    return BitConverter.ToUInt64(pathBytes, 0).ToString();
}

how would one reverse this process with a "DecodeFileHash" method?

var fileQuery = "fileone.css,filetwo.css,file3.css";
var hashedQuery = CalculateFileHash(fileQuery); // e.g. "23948759234"
var decodedQuery = DecodeFileHash(hashedQuery); // "fileone.css,filetwo.css,file3.css"

where decodedQuery == fileQuery in the end.

Is this even possible? If it isn't possible, would there by any way to generate a hash that I could easily decode?

Edit: So just to be clear, I just want to compress the variable "fileQuery" and decompress fileQuery to determine what it originally was. Any suggestions for solving that problem since hashing/decoding is out?

Edit Again: just doing a base64 encode/decode sounds like the optimal solution then.

public string EncodeTo64(string toEncode) {
    var toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
    var returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    return returnValue;
}

public string DecodeFrom64(string encodedData) {
    var encodedDataAsBytes  = System.Convert.FromBase64String(encodedData);
    var returnValue = Encoding.ASCII.GetString(encodedDataAsBytes);
    return returnValue;
}
like image 936
tester Avatar asked May 06 '11 22:05

tester


People also ask

Can you reverse an MD5 hash?

Hash functions are not reversible in general. MD5 is a 128-bit hash, and so it maps any string, no matter how long, into 128 bits. Obviously if you run all strings of length, say, 129 bits, some of them have to hash to the same value.

Can you reverse a hashing algorithm?

You can't reverse hashing functions: they are not a form of encryption. That's why hashes are always the same length regardless of the input length: they throw away information in order to generate a (hopefully) unique value from the input.

Can you reverse a cryptographic hash?

One purpose of a hash function in cryptography is to take a plaintext input and generate a hashed value output of a specific size in a way that can't be reversed.

Can you Unhash a hash?

Encryption is a two-way function; what is encrypted can be decrypted with the proper key. Hashing, however, is a one-way function that scrambles plain text to produce a unique message digest. With a properly designed algorithm, there is no way to reverse the hashing process to reveal the original password.


2 Answers

Impossible. By definition and design hashes cannot be reverted to plain text or their original input.


It sounds like you are actually trying to compress the files. If that is the case, here is a simple method to do so using GZip:

public static byte[] Compress( byte[] data )
{
    var output = new MemoryStream();
    using ( var gzip = new GZipStream( output, CompressionMode.Compress, true ) )
    {
        gzip.Write( data, 0, data.Length );
        gzip.Close();
    }
    return output.ToArray();
}
like image 113
Thomas Avatar answered Sep 20 '22 15:09

Thomas


A hash is derived from the original information, but it does not contain the original information. If you want a shorter value that hides the original information, but can be resolved to the original value your options are fairly limited:

  • Compress the original information. If you need a string, then your original information would have to be fairly large in order for the compressed, base-64-encoded version to not be even bigger than the original data.
  • Encrypt the original information - this is more secure than just compressing it, and can be combined with compression, but it's also probably going to be larger than the original information.
  • Store the original information somewhere and return a lookup key.
like image 21
Joel Mueller Avatar answered Sep 20 '22 15:09

Joel Mueller