Like Tree3Likes

String generator help

This is a discussion on String generator help within the C++ Programming forums, part of the General Programming Boards category; Hello. I am working on a project for computer security, where we are to write code in C++ that will ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    68

    String generator help

    Hello.

    I am working on a project for computer security, where we are to write code in C++ that will come up with a random string of k characters (k being 1-7, each to be done 10 times). This random string consists only of all 26 capital letters. This random string will serve as a password, and two functions will generate random passwords to compare and engage in a brute force sequential attack respectively.

    After both functions have been carried out, I am to record how many attempts it took out of how many possible attempts there are for each one, average all ten per k value, and then graph the results. This part doesn't matter too much here, since it's pretty self-explanatory.

    I have started on this project with some help from friends, who have helped me to write a random string generator, but that is the extent of our progress. The generator debugs just fine, but no results turn up. Whenever I run the code, I included a statement to see if it was coming up with anything, and all it gave me was "The string is ."

    Naturally, I'm using the same generator twice, once to come up with the password, and again to come up with random ones to compare to the password, but this leads me to the second help.

    I have no idea how to write a brute force script. Someone told me to look at it as writing a Base-26 counting machine, but that doesn't help me any. Can someone help me out with this?

    tl;dr
    1. Random String Generator not actually coming up with string. See code below
    2. How do I write a string generator that goes "A, if not A then B, if not B then C, if not C then D..." and will even increment the ten position so to speak if AZ doesn't match when I get to 2 digits, and so on.

    Code:
    // Trey Brumley
    // Topics in Computer Security
    // Dr. Nelson J. Passos
    // Class Project:  Password Cracking
    // =================================
    
    
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    
    int x;
    int stringLength = x;
    
    
    string randomStrGen(int length);
    
    
    int main()
    {
        cout << "Enter a value to serve as string length:  ";
        cin >> x;
        cout << endl << endl;
    
    
        string a;
        string b;
    
    
        a = randomStrGen(stringLength);
        int c = 0;
    
    
        cout << "The compare string is "<< a << "." << endl << endl;
    
    
        while (b != a)
        {
            c++;
            b = randomStrGen(stringLength);
        }
    
    
        cout << "It took " << c << " attempts to crack this password." << endl;
    
    
        system("pause");
        return 0;
    }
    
    
    string randomStrGen(int length)
    {
        static string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        string result;
        result.resize(length);
    
    
        for (int i = 0; i < length; i++)
            result[i] = charset[rand() % charset.length()];
    
    
        return result;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    the simplest, brute-force way to do this is with nested loops. the slightly more complex way is with recursion.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Random String Generator not actually coming up with string. See code below
    stringLength is not a reference to x, it's a separate variable that contains the value 0 throughout execution of the program. Use x instead, when calling randomStrGen.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    68
    Alright, it turned up a string, but it's turning up the same strings every time. How do I make it to where it gives me different ones? For example, it keeps giving me P as a K=1 string, and it keeps taking the same number of times (47) to crack it. So, it's not totally random. How do I make it random?

    Also, on the brute force function, how would I implement that exactly?

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    So, it's not totally random.
    It will never be totally random when using a pseudorandom number generator. However, that is a whole other issue. The issue you seem to be having now has to do with the default seed for rand always being 1. You need to reseed the generator using srand and some seed source that changes, such as a hash of the current time:
    Code:
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    
    using namespace std;
    
    namespace sonjared
        {
        unsigned hash(void *obj, size_t bytes)
            {
            unsigned h = 0;
    
            for (size_t i = 0; i < bytes; i++)
                {
                h = 33 * h + ((unsigned char*)obj)[i];
                }
    
            return h;
            }
        }
    
    int main()
        {
        time_t seed_source = time(0);
    
        srand(sonjared::hash(&seed_source, sizeof seed_source));
    
        for (int i = 0; i < 20; i++)
            {
            cout << rand() << '\n';
            }
    
        return EXIT_SUCCESS;
        }
    Aslaville likes this.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    Quote Originally Posted by Trey Brumley
    How do I make it to where it gives me different ones?
    but you said this in your first post:

    Quote Originally Posted by Trey Brumley View Post
    brute force sequential attack
    if it's sequential, then it is, by definition, not supposed to be random. which one do you want?
    Code:
    namespace life
    {
        const bool change = true;
    }

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,331
    Quote Originally Posted by Trey Brumley View Post
    Someone told me to look at it as writing a Base-26 counting machine
    One option is to use a string with 7 characters, and increment them in this order (see if you can figure out the code to do this):

    "AAAAAAA", "AAAAAAB", "AAAAAAC", ... "AAAAAAZ", "AAAAABA", "AAAAABB", "AAAAABZ", ... , "AAAAAZZ", "AAAABAA", ... "ZZZZZZZ".

    Note, this will take ~8.03 billion iterations (26^7).

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    Quote Originally Posted by rcgldr View Post
    Note, this will take ~8.03 billion iterations (26^7).
    according to the original post, the requirement is for 1-7 characters, making it more like 267 + 266 + 265 + 264 + 263 + 262 + 26 or 8,353,082,582 iterations.
    iMalc likes this.
    Code:
    namespace life
    {
        const bool change = true;
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    Quote Originally Posted by sonjared View Post
    It will never be totally random when using a pseudorandom number generator. However, that is a whole other issue. The issue you seem to be having now has to do with the default seed for rand always being 1. You need to reseed the generator using srand and some seed source that changes, such as a hash of the current time:
    Or, you know, just use the time as is:
    std::srand(static_cast<unsigned int>(std::time(nullptr));
    Simpler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    Quote Originally Posted by Elysia View Post
    Or, you know, just use the time as is:
    std::srand(static_cast<unsigned int>(std::time(nullptr));
    Simpler.
    Do that several times and keep an eye on the first number generated. Notice how predictable it is. Simply casting the time to unsigned is too simple.
    laserlight likes this.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    You can just rand a first number and the proceed to use the second generated one as the first one.
    Anyway, if you really want good distribution and uniqueness, you shouldn't be using rand anyway, but for this toy project, I think rand and a simple time should do fine. No need for complex hashing or more complex random generators.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    Quote Originally Posted by sonjared
    Do that several times and keep an eye on the first number generated. Notice how predictable it is. Simply casting the time to unsigned is too simple.
    Indeed, plus in theory, the conversion from std::time_t to unsigned int might not be well defined, whereas hashing the bytes of a std::time_t will certainly be well defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    66
    You can just rand a first number and the proceed to use the second generated one as the first one.
    While adequate, that solution smells funny. "Just throw away the first number because it is not very random" smacks of a kludge whereas generating an appropriate seed in the first place is much more elegant.

    Anyway, if you really want good distribution and uniqueness, you shouldn't be using rand anyway
    I have no argument with that. But if you are using rand, you may as well do it right.

    but for this toy project, I think rand and a simple time should do fine.
    I don't disagree, but since it is not your project, it is also not your choice. The most you or I can do is offer options for the OP to choose. If one of the options is a reasoned improvement over the usual, is that not a good thing?

    No need for complex hashing or more complex random generators.
    One could argue that the hashing I showed is far from complex, and C++ now has a random number library that includes more powerful generators with relatively simple syntax...if you do not mind calling excessive generalization and template hell "relatively simple".

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,988
    Quote Originally Posted by sonjared View Post
    While adequate, that solution smells funny. "Just throw away the first number because it is not very random" smacks of a kludge whereas generating an appropriate seed in the first place is much more elegant.
    Sure, but it also takes more time to write, test and maintain. I'd rather use a simple solution if it does the job well enough.

    I don't disagree, but since it is not your project, it is also not your choice. The most you or I can do is offer options for the OP to choose. If one of the options is a reasoned improvement over the usual, is that not a good thing?
    Indeed. You simply failed to mention the "easy" solution, which I thought was an omission, so I added it in. But, also like I said, I don't really see the need for such complex randomization here, so a simple time should suffice here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    2,579
    if you're running on linux, you can read your seed value, or even your actual random numbers, from /dev/random, but that's not a very portable solution. I'm sure other operating systems have integrated random number generation systems, but I'm just not familiar with them. keep mind that this method is prone to latency, as you're making system calls to get the random numbers
    Code:
    namespace life
    {
        const bool change = true;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Generator
    By Tonto in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2005, 01:45 AM
  2. Doc generator
    By blanc-de-poulet in forum C Programming
    Replies: 2
    Last Post: 05-13-2005, 03:35 PM
  3. .SFV Generator
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-15-2004, 01:00 PM
  4. Name Generator
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2002, 01:42 PM
  5. My Random Position Color String Generator[in C and C++!!]
    By Golden Bunny in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 05-27-2002, 06:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21