产生从一个给定长度的所有可能的串长度

2023-09-11 06:44:16 作者:素而安然

我希望能够从给定长度的所有可能的字符串,我真不知道该怎么$ C c表示$。因此,对于进一步的解释,我和一个朋友想展示一些基本的黑客技术,让暴力破解出现。当然,他会成为我的牺牲品,没有违法的事情出现。

I would like to be able to generate all possible strings from a given length, and I frankly don't know how to code that. So for further explanation, I and a friend would like to demonstrate some basic hacking techniques, so bruteforcing comes up. Of course, he will be my victim, no illegal thing there.

不过,他告诉我的唯一的事情是,他的PW将是4字符长,但我pretty的确定他的私服不会在任何dictionnary,这将是TOI容易。

However, the only thing he told me is that his PW will be 4-char-long, but I'm pretty sure his PW won't be in any dictionnary, that would be toi easy.

于是我想出了生成每4 - 焦炭 - 长串可能的想法,包含AZ字符(无上限)。

So I came up with the idea of generating EVERY 4-char-long-string possible, containing a-z characters (no caps).

会有人有导致后续到code这样的算法?我真的不与表演打扰,如果它需要1夜生成所有PW,那也没问题。

Would someone have a lead to follow to code such an algorithm ? I don't really bother with performances, if it takes 1 night to generate all PW, that's no problem.

不要忘了,只能说是对演示的目的。

Don't forget, that's only on demonstration purposes.

推荐答案

您可以这样做只是你如何用数字做。先从AAAA。然后增加了至少显著的一部分,所以AAAB。一直走,直到你到达aaaz。然后递增到AABA。重复,直到你到达ZZZZ。

You can do it just how you'd do it with numbers. Start with aaaa. Then increment the 'least significant' part, so aaab. Keep going until you get to aaaz. Then increment to aaba. Repeat until you get to zzzz.

因此​​,所有你需要做的是实施的

So all you need to do is implement is

String getNext(String current)

要扩大这一点;这可能是心不是做事情的最快捷的方式,但它是最简单的得到正确的。

To expand on this; It possibly isnt the quickest way of doing things, but it is the simplest to get right.

正如老话所说 - 先使它正确,然后使它快。获得一个工作实现,通过了所有的测试(你有检查,对吧?)是你先做。然后重写一遍,使之快,用你的测试,放心你不破的核心功能。

As the old adage goes - 'first make it right, then make it fast'. Getting a working implementation that passes all your tests (you do have tests, right?) is what you do first. You then rewrite it to make it fast, using your tests as reassurance you're not breaking the core functionality.