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 );
    }
}
22
  • 2
    Isn't a hash exactly meant to be impossible to convert back to original string? Commented Aug 29, 2018 at 7:47
  • Could you please clarify whether you want to get your original string back from the hash, or the []byte (digest) from the outputted hex string? Commented Aug 29, 2018 at 7:48
  • i want to convert an outputted hex string to ASCII Commented Aug 29, 2018 at 7:51
  • how do you want to convert for example 0xFF to ASCII? Commented Aug 29, 2018 at 7:53
  • 2
    It sounds like you're trying to revert a SHA-256 hashed string back into it's original form. This is not possible, as the SHA-256 hashing function is a so called one-way hash. You can only turn a string into a hashed string, you cannot turn the hashed string back into a normal, readable string. Commented Aug 29, 2018 at 7:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.