我已经提出了一种在C#中强制执行字符串的非常有效的方法。 (是的,它可以用作密码破解/生成器)
基本上它的工作原理如下(这个例子使用的是一个只有小写字母的5个字符长度的字符串):
此示例的代码如下所示。
using System;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
NumeralSystem sys = new NumeralSystem(9, new Range(0, Helpers.Chars.Length - 1));
sys.PrintStrings();
Console.ReadKey(true);
}
}
public class NumeralSystem
{
private int _Length;
private Range _Range;
public int Length
{
get
{
return _Length;
}
}
public Range Range
{
get
{
return _Range;
}
}
public NumeralSystem(int length, Range range)
{
_Length = length;
_Range = range;
}
public NumeralSystem(int length, int min, int max)
{
_Length = length;
_Range = new Range(min, max);
}
public void PrintStrings()
{
int num = this.Range.Max;
int[] arr = new int[this.Length];
for (int c = 0; c < arr.Length; c++) arr[c] = this.Range.Min;
while (true)
{
arr[0]++;
if (arr[0] > this.Range.Max)
{
for (int i = 1; i < arr.Length; i++)
{
if (arr[i - 1] > this.Range.Max)
{
arr[i - 1] = this.Range.Min;
arr[i]++;
}
}
}
int num2 = arr[0];
for (int i = 1; i < arr.Length; i++) num2 += arr[i];
//PrintArray(arr);
Console.WriteLine(Helpers.ArrayToString(arr));
if (num2 == this.Range.Max * arr.Length) break;
}
}
private void PrintArray(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
if (i == arr.Length - 1)
{
Console.WriteLine(arr[i]);
}
else
{
Console.Write(arr[i] + ", ");
}
}
}
}
public class Helpers
{
public static char[] Chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
public static string ArrayToString(int[] arr)
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] >= 0)
{
builder.Append(Helpers.Chars[arr[i]]);
}
else
{
throw new Exception();
}
}
return builder.ToString();
}
}
public class Range
{
private int _Max, _Min;
public int Max
{
get
{
return _Max;
}
}
public int Min
{
get
{
return _Min;
}
}
public Range(int min, int max)
{
_Max = max;
_Min = min;
}
}
那么是否有更快的方法来强制执行字符串?当我说“暴力破解一个字符串”时,我的意思是找到所有可能的字符串组合,给定长度和可能的字符。 感谢您的帮助。