C++ 中的暴力字符生成

发布于 2024-10-19 07:41:52 字数 1850 浏览 6 评论 0原文

所以我试图制作一个强力字符串生成器来匹配和比较 CUDA 中的字符串。在我开始尝试使用一门语言之前,我不知道我是否想让它在 C++ 中工作。我目前有这段代码。

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int sLength = 0;
int count = 0;
int charReset = 0;
int stop = 0;
int maxValue = 0;
string inString = "";
static const char charSet[] = //define character set to draw from
"0123456789"
"!@#$%^&*"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(charSet) - 1;


char genChars()
{
        return charSet[count]; //Get character and send to genChars()
}

int main()
{
    cout << "Length of string to match?" << endl;
    cin >> sLength;
    cout << "What string do you want to match?" << endl;
    cin >> inString;
    string sMatch(sLength, ' ');
    while(true)
    {
        for (int y = 0; y < sLength; y++)
        {
            sMatch[y] = genChars(); //get the characters
            cout << sMatch[y];

            if (count == 74)
            {
                charReset + 1;
                count = 0;
            }
            if (count == 2147000000)
            {
                count == 0;
                maxValue++;
            }
        }
        count++;
        if (sMatch == inString) //check for string match
        {
            cout << endl;
            cout << "It took " << count + (charReset * 74) + (maxValue*2147000000) << " randomly generated characters to match the strings." << endl;
            cin >> stop;
        }
        cout << endl;
    }
}

现在这段代码可以运行并编译,但它并不完全符合我的要求。它会做 4 个相同的角色,EX。 aaaa 或 1111,然后继续下一个而不像 aaab 或 1112 那样递增。我尝试过乱搞这样的事情,

for (int x = 0; x < sLength; x++)
{
    return charSet[count-sLength+x];
}

在我看来应该有效,但无济于事。

So I'm trying to make a brute force string generator to match and compare strings in CUDA. Before I start trying to mess around with a language I don't know I wanted to get one working in C++. I currently have this code.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


int sLength = 0;
int count = 0;
int charReset = 0;
int stop = 0;
int maxValue = 0;
string inString = "";
static const char charSet[] = //define character set to draw from
"0123456789"
"!@#$%^&*"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(charSet) - 1;


char genChars()
{
        return charSet[count]; //Get character and send to genChars()
}

int main()
{
    cout << "Length of string to match?" << endl;
    cin >> sLength;
    cout << "What string do you want to match?" << endl;
    cin >> inString;
    string sMatch(sLength, ' ');
    while(true)
    {
        for (int y = 0; y < sLength; y++)
        {
            sMatch[y] = genChars(); //get the characters
            cout << sMatch[y];

            if (count == 74)
            {
                charReset + 1;
                count = 0;
            }
            if (count == 2147000000)
            {
                count == 0;
                maxValue++;
            }
        }
        count++;
        if (sMatch == inString) //check for string match
        {
            cout << endl;
            cout << "It took " << count + (charReset * 74) + (maxValue*2147000000) << " randomly generated characters to match the strings." << endl;
            cin >> stop;
        }
        cout << endl;
    }
}

Now this code runs and compiles but it doesn't exactly do what I want it to. It will do 4 of the same character, EX. aaaa or 1111 and then go onto the next without incrementing like aaab or 1112. I've tried messing around with things like this

for (int x = 0; x < sLength; x++)
{
    return charSet[count-sLength+x];
}

Which in my mind should work but to no avail.

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

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

发布评论

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

评论(1

唠甜嗑 2024-10-26 07:41:52

您基本上只需要增加一个计数器,然后将计数转换为基数(字符数组的大小)。

下面是一个示例,它执行以 16 为基数的普通数字。

http://www.daniweb.com/code/snippet217243.html

您应该能够替换

   char NUMS[] = "0123456789ABCDEF";

为您的字符集并从那里找出它。这可能无法使用 uint 生成足够大的字符串,但您应该能够从那里将其分成块。

假设您的字符数组是“BAR”,因此您需要使用自己的符号而不是 0 1 和 2 转换为以 3 为基数的数字。

它的作用是执行模数来确定字符,然后除以基数,直到数变为零。相反,您要做的是重复“B”直到达到字符串长度,而不是在达到零时停止。

例如:由数字 13 生成的四个字符的字符串:

  • 14%3 = 2,因此它将把 charSet[2] 推到空字符串“R”的开头;
  • 然后它会除以 3,使用整数数学会 = 4。4%3 又是 1,所以“A”。
  • 它会再次除以 3,(1) 1%3 是 1,所以“A”。
  • 它将再次除以 3,(0)——示例将在此停止,但由于我们正在生成一个字符串,因此我们继续推入 0“B”,直到达到 4 个字符。

最终输出:BAAR

对于可以生成更大字符串的方法,您可以使用字符串大小的整数数组(称为位置),将所有整数初始化为零并执行类似的操作每次迭代时都会这样做:

   i = 0;
   positions[i]++;
   while (positions[i] == base)
   {
     positions[i] = 0;
     positions[++i]++;
   }

然后您将遍历整个数组,并使用 charSet[positions[i]] 构建字符串以确定每个字符是什么。

You basically just need to increment a counter, than convert the count number to base (size of char array)

Here's an example which does normal numbers up to base 16.

http://www.daniweb.com/code/snippet217243.html

You should be able to replace

   char NUMS[] = "0123456789ABCDEF";

with your set of characters and figure it out from there. This might not generate a large enough string using a uint, but you should be able to break it up into chunks from there.

Imagine your character array was "BAR", so you would want to convert to a base 3 number using your own symbols instead of 0 1 and 2.

What this does is perform a modulus to determine the character, then divide by the base until the number becomes zero. What you would do instead is repeat 'B' until your string length was reached instead of stopping when you hit zero.

Eg: A four character string generated from the number 13:

  • 14%3 = 2, so it would push charSet[2] to the beginning of the empty string, "R";
  • Then it would divide by 3, which using integer math would = 4. 4%3 is again 1, so "A".
  • It would divide by 3 again, (1) 1%3 is 1, so "A".
  • It would divide by 3 again, (0) -- The example would stop here, but since we're generating a string we continue pushing 0 "B" until we reach 4 our 4 characters.

Final output: BAAR

For an approach which could generate much larger strings, you could use an array of ints the size of your string, (call it positions), initialize all the ints to zero and do something like this on each iteration:

   i = 0;
   positions[i]++;
   while (positions[i] == base)
   {
     positions[i] = 0;
     positions[++i]++;
   }

Then you would go through the whole array, and build the string up using charSet[positions[i]] to determine what each character is.

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