【问题标题】:How can i make this program faster and simple?我怎样才能使这个程序更快更简单?
【发布时间】:2016-09-18 00:56:45
【问题描述】:

我是一个初学者,我制作这个程序是为了好奇“解密”sha-256 哈希需要多少时间,它变得非常复杂。这个东西可以找出8个字符的长文本,只包含数字0..9和从a到f的几个字符。

package hash;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {

public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    String[] array = new String[16];
    array[0]="0"; array[1]="1"; array[1]="1"; array[2]="2"; array[3]="3"; array[4]="4"; array[5]="5"; array[6]="6"; array[7]="7"; array[8]="8";
    array[9]="9"; array[10]="a"; array[11]="b"; array[12]="c"; array[13]="d"; array[14]="e"; array[15]="f";
    int one, two, three, four, five, six, seven, eight; one=two=three=four=five=six=seven=eight=0;
    String text = ""; //this is gonna be the original text
    String hash = "ae5ce162888ee3ebe974976cac5ab94a3f55049f8515884883d579fb3fa378d2"; //this is the hash
    byte[] digest = null;
    MessageDigest md = MessageDigest.getInstance("SHA-256");

    for (int i=1; i<999999999; i++) {

    if (i%(16*16*16*16*16*16*16)==0) {
        two=three=four=five=six=seven=eight=0; one++;
        text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
    } else {
        if (i%(16*16*16*16*16*16)==0) {
            three=four=five=six=seven=eight=0; two++;
            text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
        } else {
            if (i%(16*16*16*16*16)==0) {
                four=five=six=seven=eight=0; three++;
                text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
            } else {
                if (i%(16*16*16*16)==0) {
                    five=six=seven=eight=0; four++;
                    text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
                } else {
                    if (i%(16*16*16)==0) {
                    six=seven=eight=0; five++;
                    text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
                } else {
                    if (i%(16*16)==0) {
                    seven=eight=0; six++;
                    text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
                    } else {
                        if (i%16==0) {
                            eight=0; seven++;
                            text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
                        } else {
                            eight++;
                            text=array[one]+array[two]+array[three]+array[four]+array[five]+array[six]+array[seven]+array[eight];
                        }
                    }
                }
            }
        }
    }
}


    md.update(text.getBytes("UTF-8")); 
    digest = md.digest();


    if (String.format("%064x", new java.math.BigInteger(1, digest)).equals(hash)) {
        i=999999999;
    }
}
    System.out.println(text);
}
}

感谢您的帮助!

【问题讨论】:

  • 这可能属于 CodeReview
  • 折叠您的计算以生成数字常量。例如,您正在计算 (16*16) 999999999 次。
  • 它有 999999999 次重试,(我知道这是一种愚蠢的方式,但它只能这样工作)并且它会在找到原始文本时退出。但是 nvm 我还是后悔注册这个网站..
  • @Horten75 我有很多事情要告诉你。 (1) 您的许多安全知识/概念是错误的。 (2) 你所做的不是解密,计算每一个可能的哈希值不是解密,是反复试验。 (3) 哈希 256 输入不仅仅是来自 [0, 9] 和 [a, f]。它接受任何输入,任何大写或小写字母(HiwZ 等),符号([&#)等等。 (4) 因此,您的代码没有按您所说的那样工作。 (5)But nvm i regret sign up to this site anyway.. 这样的评论不会帮助您获得更多帮助。
  • (6) 你没看懂@nicomp 说了什么,所以再读一遍,努力学习。 (7) 老实说,你的方法很糟糕。(8) 哈希函数是为了让人们可以做你正在做的事情,如果我是你,我不会这样做,因为您添加的每 1 个额外字符会使时间增加一倍。 (9) 你甚至没有解释你的代码是如何工作的,以及你是如何尝试加速它的,所以我们不再建议同样的事情。 (10) 我写了一个 10 行代码来做你想做的事,但我不知道我是否想把它分享给这样行为的人。

标签: java


【解决方案1】:

在贴代码之前有很多话要说

  • 您提出的问题毫无意义。哈希函数是为了让您不能只计算所有哈希值并进行比较。

  • 速度取决于很多事情。如果种子是0000 不会花很长时间,如果是3Gs98y/!=p 9IYÇ]T$78fffIUas4ª!5MJg7BN Jfi86n ,K¨}]+I 可能需要几年时间。此外,计算机速度,使用的编程语言等。

  • 它们需要花费大量时间。看看这个link 了解更多详情。请注意,他们测量的是数十万年,而不是秒。

  • 这是上面链接的结论:It doesn't get much better with the fastest hardware on the planet computing thousands of hashes in parallel. No human technology will be able to crunch this number into something acceptable. So forget brute-forcing SHA-256 here

  • 以便您更好地理解:您在种子上最多使用 16 个字符,而最多可以使用 128 个不同的字符。可能的 convinations 是128^n(其中 n:种子长度)。

代码

  • 它只使用数字 [0, 9] 和字母 [A, F] (大写)

  • 要找出种子0129ABCF,需要227 seconds

  • 这段代码在很多方面仍然可以改进

    public static String calculateHash(String hash) throws NoSuchAlgorithmException, UnsupportedEncodingException{      
        MessageDigest md = MessageDigest.getInstance("SHA-256");
    
        // 16 characters used in the seed, 8 length. Possibilities : 8 * 8 * 8... (16 times) = 8 ^ 16
        long convinations = (long) Math.pow(8, 16);
        String hex, formatted;
    
        for (long i = 0; i < (long) convinations; i++) {
    
            hex = String.format("%08X", i);
    
            // This takes CPU time, only uncomment for debug purposses
            //System.out.println(hex);
    
            // Calculate hash of "hex"
            md.update(hex.getBytes("UTF-8")); 
            byte[] digest = md.digest();
    
            // Compare results
            formatted = String.format("%064x", new BigInteger(1, digest));
            if (formatted.equals(hash)) {
                return hex;
            }
        }
    
        return "";
    }
    

【讨论】:

  • 感谢您的帮助!你的代码比我的好多了,看来我还有很多东西要学:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多