Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrypt SHA1 value with C#

I have a PHP web service that I've discovered is passing my C# a SHA-1 encrupted value. The sample data that is passed to me is "8cb2237d0679ca88db6464eac60da96345513964" which I know translates to "12345".

How do I translate the hashed value back to "12345" with code similar to the following

public static string HashCode(string str)
{
string rethash = "";
try
{

      System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
       System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
       byte[] combined = encoder.GetBytes(str);
       hash.ComputeHash(combined);
       rethash = Convert.ToBase64String(hash.Hash);
}
catch (Exception ex)
{
       string strerr = "Error in HashCode : " + ex.Message;
}
return rethash;
}
  • EDIT *

Here is some RUBY code that is also workig with "8cb2237d0679ca88db6464eac60da96345513964" and "12345"

require "digest/sha1"
class User
  attr_accessor :password
  def initialize(password)
    @password = hash_password(password)
  end
  def hash_password(password)
    Digest::SHA1.hexdigest(password)
  end
  def valid_password?(password)
    @password == hash_password(password)
  end
end
u = User.new("12345")
p u.password # => "8cb2237d0679ca88db6464eac60da96345513964"
p u.valid_password?("not valid") # => false
p u.valid_password?("12345") # => true
like image 817
user2019423 Avatar asked Dec 04 '22 12:12

user2019423


2 Answers

You can't decrypt SHA1 hash because it's a one way hash.

Another example of one way hashing is MD5

like image 185
Avitus Avatar answered Dec 15 '22 05:12

Avitus


The ruby code that you posted doesn't appear to be reversing a hash.

What it seems to be doing is this:

Get the password text, hash it and store it.

Later, when it wants to check that the "user" entered the same password again, it gets the password text from the user, hashes it, and compares the hash value to the stored hash value.

This is a common way to store and check passwords. Instead of "dehashing" the stored value for comparison, you hash the new value and compare the two hash values.

like image 38
Jim Counts Avatar answered Dec 15 '22 05:12

Jim Counts