简体   繁体   中英

How can I decrypt SHA-512 hash code using salt?

I have encrypted data using sha-512 Algo and its salt , I want to decrypt that data please anyone tell me how can I decode it using Java.

Thanks

SHA-512 is a cryptographic hash function . Cryptographic hash functions are one way - you can calculate the hash for a block of data, but it is not possible to get the original data back when you have only the hash. So you cannot decrypt a hash code to get back the original data.

Cryptographic hash functions are often used to store passwords in a database. To check if the password that a user entered is correct, the first thing you might think is "Ok, so I have to decrypt the password in the database and check if it's equal to what the user entered". That is, however, not how you do this.

Instead, what you do is hash the password that the user entered, and compare that hash to the hash stored in the database. If the hashes are the same, the user entered the correct password. You don't need to "decrypt" the hash.

SHa 512 is not an encryption. Hash an algorithm that is designed to be one way only to verify the integrity of data.

So in short: No, you cannot "decrypt" any hash algorithm.

You can have Java compute a hash by using the MessageDigest class.

String plainText = "text" + "salt";

MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
byte[] hash = messageDigest.digest( plainText.getBytes() );

System.out.println("Result: " + new String(hash));

If you know the possible plainText values, you can find a matching hash. However, brute forcing this would take basically until the end of the world if you have no clue about the original data.

HOW TO IMPLEMENT SHA512 INSTEAD OF MD5

Here MD5 code

public class RijndaelSimple
{
    public static string Decrypt
    (
        string cipherText,
        string passPhrase,
        string saltValue,
        string hashAlgorithm,
        int passwordIterations,
        string initVector,
        int keySize
    )
    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);

        PasswordDeriveBytes password = new PasswordDeriveBytes
        (
            passPhrase,
            saltValueBytes,
            hashAlgorithm,
            passwordIterations
        );

        byte[] keyBytes = password.GetBytes(keySize / 8);
        RijndaelManaged symmetricKey = new RijndaelManaged();

        symmetricKey.Mode = CipherMode.CBC;


        ICryptoTransform decryptor = symmetricKey.CreateDecryptor
        (
            keyBytes,
            initVectorBytes
        );


        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);


        CryptoStream cryptoStream = new CryptoStream
        (
            memoryStream,
            decryptor,
            CryptoStreamMode.Read
        );

        byte[] plainTextBytes = new byte[cipherTextBytes.Length];


        int decryptedByteCount = cryptoStream.Read
        (
            plainTextBytes,
            0,
            plainTextBytes.Length
        );

        memoryStream.Close();
        cryptoStream.Close();

        string plainText = Encoding.UTF8.GetString
        (
            plainTextBytes,
            0,
            decryptedByteCount
        );

        return plainText;
    }
}

public class RijndaelSimpleTest
{

    [STAThread]
    static void Main(string[] args)
    {
        string plainText = "Hello, World!";    // original plaintext

        string passPhrase = "Pas5pr@se";        // can be any string
        string saltValue = "kplcs@1tValue";        // can be any string
        string hashAlgorithm = "SHA1";             // can be "MD5"
        int passwordIterations = 2;               // can be any number
        string initVector = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
        int keySize = 256;                // can be 192 or 128

        Console.WriteLine(String.Format("Plaintext : {0}", plainText));

        string cipherText = RijndaelSimple.Encrypt
        (
            plainText,
            passPhrase,
            saltValue,
            hashAlgorithm,
            passwordIterations,
            initVector,
            keySize
        );

        Console.WriteLine(String.Format("Encrypted : {0}", cipherText));

        plainText = RijndaelSimple.Decrypt
        (
            cipherText,
            passPhrase,
            saltValue,
            hashAlgorithm,
            passwordIterations,
            initVector,
            keySize
        );

        Console.WriteLine(String.Format("Decrypted : {0}", plainText));}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM