在C#中生成顺序密码

时间:2013-12-06 18:05:00

标签: c# hash switch-statement

我一直在尝试在C#(aaaa,aaab,aaac,... abcd,abce)中依次为哈希破解者生成密码(用于白帽)。但是,我不知道该怎么做。

现在我有一个char数组,其最后一个元素由一个开关递增:

            switch (character) {
            case ('0'):
                character = '1';
                break;
            case ('1'):
                character = '2';
                break;
            case ('2'):
                character = '3';
                break;
            case ('3'):
                character = '4';
                break;
            case ('4'):
                character = '5';
                break;
            case ('5'):
                character = '6';
                break;
            case ('6'):
                character = '7';
                break;
            case ('7'):
                character = '8';
                break;
            case ('8'):
                character = '9';
                break;
            case ('9'):
                character = 'a';
                break;
            case ('a'):
                character = 'b';
                break;
            case ('b'):
                character = 'c';
                break;
            case ('c'):
                character = 'd';
                break;
            case ('d'):
                character = 'e';
                break;
            case ('e'):
                character = 'f';
                break;
            case ('f'):
                character = 'g';
                break;
            case ('g'):
                character = 'h';
                break;
            case ('h'):
                character = 'i';
                break;
            case ('i'):
                character = 'j';
                break;
            case ('j'):
                character = 'k';
                break;
            case ('k'):
                character = 'l';
                break;
            case ('l'):
                character = 'm';
                break;
            case ('m'):
                character = 'n';
                break;
            case ('n'):
                character = 'o';
                break;
            case ('o'):
                character = 'p';
                break;
            case ('p'):
                character = 'q';
                break;
            case ('q'):
                character = 'r';
                break;
            case ('r'):
                character = 's';
                break;
            case ('s'):
                character = 't';
                break;
            case ('t'):
                character = 'u';
                break;
            case ('u'):
                character = 'v';
                break;
            case ('v'):
                character = 'w';
                break;
            case ('w'):
                character = 'x';
                break;
            case ('x'):
                character = 'y';
                break;
            case ('y'):
                character = 'z';
                break;
            case ('z'):
                character = 'A';
                break;
            case ('A'):
                character = 'B';
                break;
            case ('B'):
                character = 'C';
                break;
            case ('C'):
                character = 'D';
                break;
            case ('D'):
                character = 'E';
                break;
            case ('E'):
                character = 'F';
                break;
            case ('F'):
                character = 'G';
                break;
            case ('G'):
                character = 'H';
                break;
            case ('H'):
                character = 'I';
                break;
            case ('I'):
                character = 'J';
                break;
            case ('J'):
                character = 'K';
                break;
            case ('K'):
                character = 'L';
                break;
            case ('L'):
                character = 'M';
                break;
            case ('M'):
                character = 'N';
                break;
            case ('N'):
                character = 'O';
                break;
            case ('O'):
                character = 'P';
                break;
            case ('P'):
                character = 'Q';
                break;
            case ('Q'):
                character = 'R';
                break;
            case ('R'):
                character = 'S';
                break;
            case ('S'):
                character = 'T';
                break;
            case ('T'):
                character = 'U';
                break;
            case ('U'):
                character = 'V';
                break;
            case ('V'):
                character = 'W';
                break;
            case ('W'):
                character = 'X';
                break;
            case ('X'):
                character = 'Y';
                break;
            case ('Y'):
                character = 'Z';
                break;
            case ('Z'):
                character = '@';
                break;
            case ('@'):
                character = '%';
                break;
            case ('%'):
                character = '/';
                break;
            case ('/'):
                character = '\\';
                break;
            case ('\\'):
                character = '\'';
                break;
            case ('\''):
                character = '!';
                break;
            case ('!'):
                character = '$';
                break;
            case ('$'):
                character = '#';
                break;
            case ('#'):
                character = '^';
                break;
            case ('^'):
                character = '?';
                break;
            case ('?'):
                character = ':';
                break;
            case (':'):
                character = ',';
                break;
            case (','):
                character = '(';
                break;
            case ('('):
                character = ')';
                break;
            case (')'):
                character = '[';
                break;
            case ('['):
                character = ']';
                break;
            case (']'):
                character = '{';
                break;
            case ('{'):
                character = '}';
                break;
            case ('}'):
                character = '-';
                break;
            case ('-'):
                character = '_';
                break;
            case ('_'):
                character = '+';
                break;
            case ('+'):
                character = '=';
                break;
            case ('='):
                character = '0';
                break;
                prev = true;
            default:
                character = '0';
                break;
        }

但是,当该元素再次达到0时,我需要增加密码的前一个数字,如果该数字也达到0,我需要从该数字增加之前的数字,所以上。

此外,由于这是一个哈希破解者,它需要很快。有什么建议吗?

7 个答案:

答案 0 :(得分:6)

我的第一个答案不会给你你想要的东西。这将:

创建一个定义字母表的字符串:

const string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";

然后使用all -1初始化一个int数组,只要你的密码最长。这个最多可以包含8个字符:

int[] pwArray = new int[] {-1, -1, -1, -1, -1, -1, -1, -1};

然后运行一个循环:

for (int i = 0; i < Whatever; ++i)
{
    string password = ToPasswordString(pwArray, Alphabet);
    // do something with the password
}

string ToPasswordString(int[] pass, string alphabet)
{
    for (int i = pass.Length-1; i > 0; --i)
    {
        pass[i]++;
        if (pass[i] < alphabet.Length)
        {
            break;
        }
        pass[i] = 0;
    }
    var sb = new StringBuilder();
    for (int i = 0; i < pass.Length; ++i)
    {
        if (pass[i] >= 0)
            sb.Append(alphabet[pass[i]]);
    }
    return sb.ToString();
}

这里的关键是我们不修改密码字符。相反,我们将索引修改为字符数组。

当然,这不会像其他一些方法(每秒密码)那么快,但是你的程序速度受限于你被黑客攻击的网站响应登录尝试的速度。与此相比,您花费在生成密码上的时间是无关紧要的。

答案 1 :(得分:5)

为什么不直接将int值映射到char,而不是复杂且非常大的switch语句。

for (int i = 0; i < (int)Char.MaxValue; i++) { 
  char c = (char)i;
  ...
}

这样可以很容易地构建暴力密码生成器。

答案 2 :(得分:1)

您可以使用ascii代码作为示例:

public char GetNextChar(char c)
{
   return (char)(((int)c) + 1);
}

对于示例来说,这将是一个很好的扩展:

public static class Extensions
{
   public static char GetNextChar(this char c)
   {
      return (char)(((int)c) + 1);
   }
}

并使用它:

character = character.GetNextChar();

答案 3 :(得分:1)

这样的事情应该有效。

char[] currPassword = new char[] { (char)65, (char)65, (char)65, (char)65, (char)65 }; //65 = A
const int Z = 90;
const int A = 65;

void Main()
{
    while(true)
    {
        string curr = new string(currPassword);
        Next();
    }
}

public void Next()
{
    int currIndex = currPassword.Length -1;
    bool done = false;

    do
    {
        currPassword[currIndex]++;

        if(currPassword[currIndex] > Z && currIndex >= 0) 
        {
            currPassword[currIndex] = (char)A;
            currIndex--;
        }
        else
        {
            done = true;
        }
    }while(!done);
}

答案 4 :(得分:0)

LINQLIB包含计算所有排列或组合的方法。您可以使用它,尽管订单可能与您在问题中描述的完全不同。

答案 5 :(得分:0)

另一种不同的方法:

static void Main(string[] args)
        {
            string charSet = "0123456789!@#$%^&*";

            string password = "20@";
            StringBuilder start = new StringBuilder("0");
            int j = 0;
            int z = 0;
            while (start.ToString() != password)
            {
                start[z] = charSet[j++];
                Console.WriteLine(start);
                if (j == charSet.Length)
                {
                    if (start.ToString().Where(c => c == charSet[charSet.Length - 1]).Count() == start.ToString().Length)
                    {
                        start.Append("0");
                        for (int t = 0; t < start.Length; t++)
                        {
                            start[t] = '0';

                        }
                        z++;
                    }
                    else
                    {
                        for (int t = start.Length - 2; t >= 0; t--)
                        {
                            if (charSet.IndexOf(start[t]) == charSet.Length - 1)
                            {
                                start[t] = '0';
                            }
                            else
                            {
                                start[t] = charSet[charSet.IndexOf(start[t]) + 1];
                                break;
                            }
                        }
                    }
                    j = 0;
                }

            }
        }

答案 6 :(得分:0)

只需将每个密码条目视为数字,其基数是初始词典中存在的字符数。每当你在给定位置达到最大值时,你就会'继续'到下一个'数字'。

这个小班将返回所有可能的组合,给出:

  • 初始字符词典
  • 最小密码大小
  • 最大密码大小

请记住,这样的序列非常快,非常快,具体取决于初始字典大小。例如,以下具有默认值(minChar = 2,MaxChar = 4)的代码生成866495个条目。

public class Sequencer
{
    public string characterDictionary = "0123456789";
    //public string characterDictionary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-=[];',./`~!@#$%^&*()_+{}|:\"<>?|\\";
    public int minCharCount = 2;
    public int maxCharCount = 4;

    private List<string> _sequence;


    public List<string> GetSequence()
    {

        _sequence = new List<string>();


        for (int i = minCharCount; i < (maxCharCount + 1); i++)
            RenderCombinations(i);

        return _sequence;
    }

    private void RenderCombinations(int charCount)
    {
        int _dictSize = characterDictionary.Length;
        int[] _containerMatrix = new int[charCount];
        char[] _splitDict = characterDictionary.ToCharArray();

        bool _maxReached = false;

        do
        {
            string _currentCombination = "";

            for (int i = 0; i < charCount; i++)
                _currentCombination += _splitDict[_containerMatrix[i]];

            _sequence.Add(_currentCombination);

            // Let the shifting begin!

            bool _mustCarry = false;
            int _carryIndex = 0;

            do
            {
                _mustCarry = false;

                if (_carryIndex == _containerMatrix.Length)
                {
                    _maxReached = true;
                    break;
                }

                _containerMatrix[_carryIndex]++;

                if (_containerMatrix[_carryIndex] == _dictSize)
                {
                    _mustCarry = true;
                    _containerMatrix[_carryIndex] = 0;
                }

                _carryIndex++;

                if (_carryIndex > charCount)
                {
                    _mustCarry = false;
                    _maxReached = true;
                }
            } while (_mustCarry);
        } while (!_maxReached);
    }
}