Java 解密 SHA 加密字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16231643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:54:36  来源:igfitidea点击:

Decrypt SHA encrypted string

javaencryptionsha

提问by Gapchoos

I have encrypted a string using the above code.

我使用上面的代码加密了一个字符串。

public String encrypt(String generatedKey)
    {
            try {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(generatedKey.getBytes("UTF-8"));
                byte digest[] = md.digest();
                return (new BASE64Encoder()).encode(digest);
            }
            catch (Exception e) {
                return null;
            }

    }

Similarly i need a code to decrypt the above generated code. How can i do this?

同样,我需要一个代码来解密上面生成的代码。我怎样才能做到这一点?

回答by venki

I am pretty sure that we cannot decode a SHA encrypted string directly.

我很确定我们不能直接解码 SHA 加密的字符串。

See this for a clear explanation: How to decrypt SHA-256 encrypted String?

请参阅此以获得明确的解释:如何解密 SHA-256 加密的字符串?

回答by Mehmet Ata?

SHA is a digest algorithm, not an encryption algorithm. Digest values are not decryptable. That's why they are secure. Two different inputs may give same digest values. But it is a very little possibility. For sha256 it is 1/(2^256).

SHA 是摘要算法,而不是加密算法。摘要值不可解密。这就是为什么它们是安全的。两个不同的输入可能给出相同的摘要值。但这种可能性很小。对于 sha256,它是 1/(2^256)。

Output of digest algorithms have constant length. For SHA256 it is always 256 bit, regardless of your input length, 1 bit or 100 Gbs. If we could decrypt 256 bit digest value and have the original 1Gb input back, we would never need compression algorithms :)

摘要算法的输出具有恒定长度。对于 SHA256,它始终为 256 位,无论您的输入长度是 1 位还是 100 Gbs。如果我们可以解密 256 位摘要值并返回原始 1Gb 输入,我们将永远不需要压缩算法:)

回答by heffaklump

Message digests produce a small "fingerprint" of a larger set of data. It's a one way procedure.

消息摘要生成较大数据集的小“指纹”。这是一种单向程序。

What you probably is looking for is encryption.

您可能正在寻找的是加密。

Key key = new SecretKeySpec(secret.getBytes(), ALGORITHM);

// Encrypt
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(plainText);

// Decrypt
cipher.init(Cipher.DECRYPT_MODE, key)
byte[] decryptedData = cipher.doFinal(encryptedData);

Where ALGORITHM can be one of http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#Cipher

其中 ALGORITHM 可以是 http://docs.oracle.com/javase/6/docs/technotes/guides/security/StandardNames.html#Cipher 之一