0

I have the following code and I want a Java string from a SHA256 hash string. Is there a way to convert hex string to its original value?

public class CryptoHash {
    public static void main( String[] args ) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance( "SHA-256" );
        String text = "Text to hash, cryptographically.";

        // Change this to UTF-16 if needed
        md.update( text.getBytes( StandardCharsets.UTF_8 ) );
        byte[] digest = md.digest();

        String hex = String.format( "%064x", new BigInteger( 1, digest ) );
        System.out.println( hex );
    }
}

1 Answers1

1

Simple Answer: No, there is no way.

Longer Answer: There may be a way using a Brute Force Strategy. But it would take a long time, too long to be efficient.

See, Hash in general is made to make it impossible to reverse. You convert the String into some Hash and this cannot be reversed. You may take a look at how Hashes and Encrypting work.

https://www.cryptocompare.com/coins/guides/how-does-a-hashing-algorithm-work/

If it would be that easy, using just one line of code, the whole idea and process of hashing would be problematic.

Edit: If you want to convert a String to Hash, then to Hex and finally to ASCII your result will be the original Hash. Therefore, the idea won't work out. You cannot decrypt it this way.

ProgFroz
  • 197
  • 4
  • 17