-2

I have encrypted my password(Password1234$) using SecureSHA1and got the following

Output: SHA1 hash for Password1234$ is 12a00057ae853a05601d5fafb7bc8141c37eaabe.

I want to write a Decryption Method for the same so that if i pass my encrypted string 12a00057ae853a05601d5fafb7bc8141c37eaabe. i need to get my password Password1234$

Below is my code which i used to Encrypt the Password.

SecureSHA1.java

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SecureSHA1 {
    private static String convertToHex(byte[] data) { 
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) { 
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do { 
                if ((0 <= halfbyte) && (halfbyte <= 9)) 
                    buf.append((char) ('0' + halfbyte));
                else 
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while(two_halfs++ < 1);
        } 
        return buf.toString();
    }


    public static String getSHA1(String text) 
                throws NoSuchAlgorithmException, UnsupportedEncodingException  { 
        MessageDigest md;
        md = MessageDigest.getInstance("SHA-1");
        byte[] sha1hash = new byte[40];
        md.update(text.getBytes("iso-8859-1"), 0, text.length());
        sha1hash = md.digest();
        return convertToHex(sha1hash);
    } 

    public static void main(String args[]){
        try {
            String s = "Password1234$";
            String sha1S = getSHA1(s);
            System.out.print("SHA1 hash for "+ s+ " is "+ sha1S);
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}
09Q71AO534
  • 4,300
  • 13
  • 44
  • 68

2 Answers2

4

There is a big difference between encryption and hashing. Encryption is designed so you encode and decode something using a key.

Hashing is specifically designed to be a one-way calculation. You can't get the input from the output, except by brute force.

SHA1 is a hashing algorithm.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
  • Do we have any Brute Force Algorithm implemented. – 09Q71AO534 Nov 25 '15 at 06:26
  • A brute force approach would be to simple try to hash any possible input value until you create the same hash. If the input is short, like passwords, a list of pre-computed hashes helps. That's called a rainbow table. That is also a reason why you don't just hash password, but use a "salt". (google for "rainbow table" and "password salt" to learn more) – Thomas Stets Nov 25 '15 at 06:35
  • Thanks for the suggestions ;) – 09Q71AO534 Nov 25 '15 at 08:47
1

The answer is that you cannot reverse an SHA1 hash!

See this post for further details: Is it possible to reverse a sha1?

Community
  • 1
  • 1
Imran Ahmed
  • 790
  • 1
  • 9
  • 22