简体   繁体   English

Java中的密码加密/解密

[英]Password encryption/decryption in Java

I found this piece of code that returns the checksum for a given String. 我发现这段代码返回给定String的校验和。

public static String getChecksum(String md5) {
    int counter = 0;
    while (counter != 2) {
        try {
            java.security.MessageDigest md = java.security.MessageDigest
                    .getInstance("MD5");
            byte[] array = md.digest(md5.getBytes());
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < array.length; ++i) {
                sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
                        .substring(1, 3));
            }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
            continue;
        }
    }
    return null;
}

I would now like to do the opposite- ie given the checksum, get the original String back. 我现在想做相反的事情-即给定校验和,取回原始的String。 How is this possible? 这怎么可能?

You cannot. 你不能。 The point of a cryptographic hash function (or a " MessageDigest ") is that it is one-way - there is no known method to reverse the hash, besides brute force (where you hash every possible password, and see which one gives you the same hash). 加密哈希函数(或“ MessageDigest ”)的重点是它是单向的-除了蛮力(您在其中对每个可能的密码进行哈希,然后看看哪一个给您提供密码)之外,没有已知的方法来反转哈希。相同的哈希值)。

If you want to reverse the process, then you are not looking for a hash function. 如果要撤消该过程,则不需要寻找哈希函数。

A hash is a one-way cryptographic function, it has been purposefully and deliberately designed so that you can never, ever retrieve the plaintext given a hash. 散列是一种单向加密功能,它是经过有意和有意设计的,因此您永远无法获取给定散列的纯文本。

You can play games using dictionaries etc. to try to guess what the hash was... but you can't "decrypt" it. 您可以使用字典等玩游戏,尝试猜测哈希是什么...但是您不能“解密”它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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