Is it possible to decrypt some string which was earlier encrypted with the SHA-1 algorithm in Java?
解决方案
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.