Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java convert string to md5 and vise versa

Tags:

java

md5

I have java three strings. let say example.

String test = "Hi, ";
String test1 = "this is ";
String test2 = "Java programming!";

I wanna combine those 3 string and change it to md5 format. How could I do? I use MessageDigest class for one string but i have no idea how to append 3 string before change to md5. And I wanna to change back md5 to string. Do i need external library.

like image 875
sai aung myint soe Avatar asked Dec 24 '22 01:12

sai aung myint soe


1 Answers

Well reversing MD5 is not really feasible as -

  • There can be more than one string giving the same MD5 (It is called a Hash collision.)
  • It was designed to be hard to "reverse"

You would have seen lot of websites that provide reverse MD5 (as - Option-1 , option-2). These websites stores mapping of already used "String and MD5" (So if you use complex String they wont be able to deduce original one).

Moving back to Part - 1

MessageDigest can be used to calculate MD5 of a given String in Java. Its usage is fairly Simple -

        String testString="someText";
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] messageDigest = md.digest(testString.getBytes());
        BigInteger number = new BigInteger(1, messageDigest);
        String hashtext = number.toString(16);

So in your problem it depends how you want to create hash -

Way 1 - As you asked we can have -

StringBuilder simpleString=new StringBuilder(test);
simpleString.append(test1);
simpleString.append(test2);
String testString=simpleString.toString();

And finally call the method defined above with input as - testString.

Way 2 - I will suggest you to use MD5 of MD5 to have secured checksum.

Output = MD5(MD5(test)+MD5(test1)+MD5(test2))

You can try creating a brute-force attack on a string of 3 characters. Assuming only English letters (A-Z, a-z) and numbers (0-9) are allowed, there are "only" 623 (238,328) combinations in this case.

Hope it helps. :)

like image 62
freesoul Avatar answered Jan 02 '23 08:01

freesoul