java 在java中解码md5加密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40503201/
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-11-03 05:16:57  来源:igfitidea点击:

decode md5 ecnrytion in java

javaencryptionmd5

提问by

public static String convertToMD5(String input) throws Exception {
    String md5 = null;
    if (null == input)
        return null;
    try {
        // Create MessageDigest object for MD5
        MessageDigest digest = MessageDigest.getInstance("MD5");
        // Update input string in message digest
        digest.update(input.getBytes(), 0, input.length());
        // Converts message digest value in base 16 (hex)
        md5 = new BigInteger(1, digest.digest()).toString(16);
    } catch (NoSuchAlgorithmException e) {

        throw e;
    }
    return md5;
}

using this code to encrypt the string i want to decode md5 encryption to normal string? can you help

使用此代码加密字符串我想将 md5 加密解码为普通字符串?你能帮我吗

采纳答案by px06

To add onto @dit's answer, you only have one option; which is to compare MD5 strings, for example. MD5("cat") == MD5("cat"), there is no way to derive "cat"from MD5("cat")because as explained it's a hash function.

要添加@dit 的答案,您只有一种选择;例如,这是比较 MD5 字符串。MD5("cat") == MD5("cat"),没有办法推导"cat"出来,MD5("cat")因为正如解释的那样,它是一个散列函数。

Here is something you can use for comparison:

您可以使用以下内容进行比较:

public static boolean matching(String orig, String compare){
    String md5 = null;
    try{
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(compare.getBytes());
        byte[] digest = md.digest();
        md5 = new BigInteger(1, digest()).toString(16);

        return md5.equals(orig);

    } catch (NoSuchAlgorithmException e) {
        return false;
    }

    return false;
}

Then you can call matching("d077f244def8a70e5ea758bd8352fcd8", "cat");which will return true, and if matching(MD5("x"), "y")it will return false.

然后你可以调用matching("d077f244def8a70e5ea758bd8352fcd8", "cat");which will return true,如果matching(MD5("x"), "y")它会返回 false 。

回答by dieter

md5 is not a encryption algorithm. It is a hash function. The hashed string can not be decoded. The original string is "destroyed" / hashed forever.

md5 不是加密算法。它是一个散列函数。散列字符串无法解码。原始字符串被“销毁”/永远散列。