找出一个可变长度的字符串?

发布于 2024-11-07 04:36:10 字数 677 浏览 0 评论 0原文

我有一个字符串的 MD5 哈希值,存储为 String。我正在编写一个小程序来通过暴力找出原始字符串。

我想循环遍历 char 的子集。

下面的代码适用于 String.length() == 0 的情况。

我不知道如何编辑此代码以使用可变长度 String 。我觉得我在递归方面走在正确的轨道上,但不能再进一步了。

我有以下代码:

    public void attempt(String initial, String md5) {

    for (char c = ' '; c < '~'; ++c) {
        attempt = initial + Character.toString(c);
        generatedMd5 = generateMD5(attempt);
        System.out.println(attempt);
        if (hashesMatch(md5, generatedMd5)) {
            break;
        } else attempt(attempt, md5);
    }
}

注意:我应该提到这是针对 MD5 的学术研究。

I have the MD5 hash for a String, stored as a String. I'm writing a small program to find out the original string by brute force.

I'd like to loop through a subset of char.

The code below works for when String.length() == 0.

I can't figure out how to edit this code to work with variable-length Strings. I feel like I'm on the right track with recursion, but can't go any further.

I have the following code:

    public void attempt(String initial, String md5) {

    for (char c = ' '; c < '~'; ++c) {
        attempt = initial + Character.toString(c);
        generatedMd5 = generateMD5(attempt);
        System.out.println(attempt);
        if (hashesMatch(md5, generatedMd5)) {
            break;
        } else attempt(attempt, md5);
    }
}

Note: I should mention this is for an academic study on MD5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁的年少不轻狂 2024-11-14 04:36:10

您正在进行“深度优先”搜索(并且深度无限!),这是如果您不添加一些深度检查,几乎肯定会失败(耗尽您的堆栈)。

应该更好可能想要进行广度优先搜索 :您的循环应首先尝试导致添加字符的所有组合,然后,如果没有成功,请尝试使用每个增强字符串递归调用该方法。

无论如何,您应该始终添加一些深度检查。

编辑:三思而后行,我不太确定你不应该坚持深度第一。广度优先仅适用于小深度和组合(字符范围)。可能的实施

  // Breadth first returns null if not found
  public String bruteforce(List<String> prefixes, String md5,int availdepth) {
    if(availabledepth<0) return null;
    List<String> newprefixes = new ArrayList<String>();
    for(String prefix : prefixes) {
        for (char c = ' '; c < '~'; ++c) {
          String attempt = prefix + Character.toString(c);
          generatedMd5 = generateMD5(attempt);
          if (hashesMatch(md5, generatedMd5)) 
            return attempt;
          newprefixes.add(attempt);
       }
    }
    // no success in this level go for next
    return bruteforce(newprefixes,md5,availddepth-1);
  }


  // Depth first - returns null if not found
  public String bruteforce(String prefix, String md5,int availdepth) {
    if(availdepth <= 0) return null;
    for (char c = ' '; c < '~'; ++c) {
          String attempt = prefix + Character.toString(c);
          if (hashesMatch(md5, generateMD5(attempt))) 
            return attempt;
          String res = bruteforce(attempt, md5, availdepth-1);
          if(res != null) return res;
       }
    return null;
  }

You are doing a "Depth first" search (and of unlimited depth!), this is almost guaranteed to fail ( exhausting your stack) if you do not add some depth check.

You should probably better might want to do a Breadth first search : your loop should first try all the combinations that results in adding a character, and only then, if no success, try to recursively call the method with each augmented string.

In any case, you should add some depth check, always.

Edited: thinking it twice, I'm not so sure you should not stick with depth first. Breadth first is only rasonable here for small depths and combinations (character ranges). A possible implementation

  // Breadth first returns null if not found
  public String bruteforce(List<String> prefixes, String md5,int availdepth) {
    if(availabledepth<0) return null;
    List<String> newprefixes = new ArrayList<String>();
    for(String prefix : prefixes) {
        for (char c = ' '; c < '~'; ++c) {
          String attempt = prefix + Character.toString(c);
          generatedMd5 = generateMD5(attempt);
          if (hashesMatch(md5, generatedMd5)) 
            return attempt;
          newprefixes.add(attempt);
       }
    }
    // no success in this level go for next
    return bruteforce(newprefixes,md5,availddepth-1);
  }


  // Depth first - returns null if not found
  public String bruteforce(String prefix, String md5,int availdepth) {
    if(availdepth <= 0) return null;
    for (char c = ' '; c < '~'; ++c) {
          String attempt = prefix + Character.toString(c);
          if (hashesMatch(md5, generateMD5(attempt))) 
            return attempt;
          String res = bruteforce(attempt, md5, availdepth-1);
          if(res != null) return res;
       }
    return null;
  }
软的没边 2024-11-14 04:36:10

首先你不返回结果...

其次你在无限空间上进行深度优先(它永远不会结束...)

public String attempt(String initial, String md5,int depth) {
    if(depth < 0)return null;//failed backtrack
    for (char c = ' '; c < '~'; ++c) {
        attempt = initial + Character.toString(c);
        generatedMd5 = generateMD5(attempt);
        System.out.println(attempt);
        if (hashesMatch(md5, generatedMd5)) {
            return attempt;//success
        } else {
            String res = attempt(attempt, md5,depth-1);
            if(res!=null)return res;
        }
    }
}

这是一个有界深度优先意味着它不会递归得比深度更远(并返回null 当它找不到任何东西时,

以下可用于遍历整个空间,深度可达 10000

String attempt(String initial,String md5){
    for(int i = 5;i<10000;i++){
        String res = attempt(initial,md5,i);
        if(res!= null)return res;
    }
    return null;//let's not go overboard on recursion
}

我将递归的最大大小设置为 10000,以使其在可预见的将来的某个时候结束

first of you don't return a result...

second you are doing depth first on a infinite space (it's never going to end...)

public String attempt(String initial, String md5,int depth) {
    if(depth < 0)return null;//failed backtrack
    for (char c = ' '; c < '~'; ++c) {
        attempt = initial + Character.toString(c);
        generatedMd5 = generateMD5(attempt);
        System.out.println(attempt);
        if (hashesMatch(md5, generatedMd5)) {
            return attempt;//success
        } else {
            String res = attempt(attempt, md5,depth-1);
            if(res!=null)return res;
        }
    }
}

this is a bounded depth first meaning it won't recurse further than depth (and return null when it can't find anything

the following can be used to go over the entire space up to depth 10000

String attempt(String initial,String md5){
    for(int i = 5;i<10000;i++){
        String res = attempt(initial,md5,i);
        if(res!= null)return res;
    }
    return null;//let's not go overboard on recursion
}

I put a max size on the recursion on 10000 to make it end sometime in the foreseeable future

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文