How to decrypt MD5 in Ruby? [duplicate] How to decrypt MD5 in Ruby? [duplicate] ruby ruby

How to decrypt MD5 in Ruby? [duplicate]


MD5 is a hashing algorithm, you can not easily decrypt the output back to the original (and this is exactly why we use hashing algorithms).

A common example is passwords. Instead of storing a password in your database, you generate a MD5 hash of the original password and store it. If one day someone steals your database it's harder for them to figure out the real passwords, as they can not be directly decrypted.

But when you're trying to login a user, he's going to type the real password, you will then run the MD5 algorithm again and compare the hash with the one you have stored, if they're the same then the user possibly typed the correct password.


You can't 'decrypt' it, as it was never encrypted. Trapdoor hashing algorithms may map multiple distinct strings to the same key (a 'collision'). MD5 is such an algorithm.

Consider a much simpler hashing algorithm:

def dumb_hash( str )  str.each_byte.inject(0) do |hash,c|     ( hash + c ) % 10  endendp dumb_hash( "hello world" )   #=> 6p dumb_hash( "hi there kids" ) #=> 6

This algorithm produces a very large number of hash collisions, unlike MD5; but still, you can't 'decode' 6 to "hello world". The best you can do is to hash your own strings until you find one that produces the same result (and hope that this is what the original was).

…That said, however, large databases exist that have already encrypted hashed huge amounts of strings and associated them with their MD5 hash. For example, if you type 70483b6e100c9cebbffcdc62dea07eda into http://www.md5decrypter.co.uk/, you do get "Jose" back.

If you wanted to do this in Ruby, what you would do is something like the following:

require 'digest'require 'sequel' # http://sequel.rubyforge.org/DB = Sequel.sqlite 'md5s.db'DB.create_table? :hashes do  String :original  String :hashend# Fill the DB with whatever is on each line of the fileIO.foreach( 'my_gigabytes_of_words.txt' ) do |line|  line.chomp!  DB[:hashes] << { original:line, hash:Digest::MD5.hexdigest(line) }end# Read from the DBdef find_originals( hash )  DB[:hashes].filter( hash:hash ).map(:original)endp find_originals("70483b6e100c9cebbffcdc62dea07eda")#=> ["Jose"]#=> ...if that was in your gigabytes of source words


MD5 is a one-way hash encryption algorithm. There is no way to directly decrypt a MD5 hash. The algorithm itself uses modular arithmetic to package the serialized string and there is no way to go backwards from that. Think about it, if you have 6 % 4 = 2 and then you have 9 % 7 = 2 then both results end up as 2. This is kinda how md5 works.

The only way to get some form of un-encrypting a md5 digest is to compare other hashes to it. So does md5("me") == hash, then hash decrypted = "me". This form of a lookup uses a rainbow table. You may have some luck looking online for a "md5 rainbow table lookup".

Here is an example md5 lookup website:

http://www.md5decrypter.co.uk/