How to decrypt sha1-encrypted String in Java

前端 未结 3 757
眼角桃花
眼角桃花 2021-01-05 01:47

Is it possible to decrypt some string which was earlier encrypted with the SHA-1 algorithm in Java?

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 02:00

    SHA1 is a cryptographic hash function, and the entire point is that you can't undo it. If it was possible to reverse the hash (find the input for a given hash), it wouldn't be useful. If you need to encrypt something and later decrypt it, you should use an encryption function like AES or RSA.

    However, for very simple inputs it may be possible to crack the hash function by guessing what the input was and checking if the hash is the same.

    Example Python code:

    def crack_hash(hash_to_crack, hash_function, list_of_guesses):
        # Try to hash everything in our guess list
        for guess in list_of_guesses:
            new_hash = hash_function(guess)
            # if the hashes match, we found it
            if new_hash == hash_to_crack:
                return guess
        # If none of them match, give up
        return None
    

    Of course, if you actually want to crack hashes efficiently, using software like John the Ripper or Hashcat is probably your best bet. Note that this usually works on passwords since they're short and easy to guess, but the difficulty increases exponentially as the input increases. You can crack every SHA-1 hash with a 6-character input in minutes, while cracking one with 16 characters would take trillions of years on average.

提交回复
热议问题