找出一个可变长度的字符串?
我有一个字符串的 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 String
s. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在进行“深度优先”搜索(并且深度无限!),这是如果您不添加一些深度检查,几乎肯定会失败(耗尽您的堆栈)。
您
应该更好可能想要进行广度优先搜索 :您的循环应首先尝试导致添加字符的所有组合,然后,如果没有成功,请尝试使用每个增强字符串递归调用该方法。无论如何,您应该始终添加一些深度检查。
编辑:三思而后行,我不太确定你不应该坚持深度第一。广度优先仅适用于小深度和组合(字符范围)。可能的实施
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 bettermight 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
首先你不返回结果...
其次你在无限空间上进行深度优先(它永远不会结束...)
这是一个有界深度优先意味着它不会递归得比深度更远(并返回null 当它找不到任何东西时,
以下可用于遍历整个空间,深度可达 10000
我将递归的最大大小设置为 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...)
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
I put a max size on the recursion on 10000 to make it end sometime in the foreseeable future