Last Updated:

Password Generator in C++ and a Little Bit About Network Security

We use passwords very often, but we don't always think about what a strong password should be. Especially if there is any confidential information hidden under the password, it is very important that no one except me can access this information. First, a few network security rules:
  • The password must be difficult for brute force to guess.
  • Passwords should be different for all web services.
  • Never save your passwords in your browser.
  • Only 1 person should know the answer to the secret question, i.e. myself.

Often, it is problematic to come up with a complex password for each web service, because a person's imagination is not unlimited. Therefore, a good way is to use a complex password generator.

Recently I had a need and I wrote such a generator. I specifically post the source code of the program, because I believe that the software should be only with open source.

Also, you will not doubt that after generating the password, it will not be sent to the database for brute force or to receive md5-hash. In general, you can mock the code as much as you can imagine. Actually, the source itself:

Listing passgen.cpp

#include <iostream>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <fstream>

using std::cout;
using std::cin;
using std::endl;

class PassGen {

public:
    void displayMessage()
    {
        int passLenght;
        int numOfPasswords;
        char * filename = new char;

        cout << "Enter the length of the password to generate: ";
        cin >> passLenght;
        cout << "Enter the number of passwords to generate: ";
        cin >> numOfPasswords;
        cout << "Passwords will be generated: " << numOfPasswords << "." <<endl;
        cout << endl;
        cout << "Enter filename to write: ";
        cin >> filename;

        std::ofstream outFile(filename);

        for (int k = 0; k < numOfPasswords; k++) {
            for (int i = 0; i < passLenght; ++i) {
                numOfChars(passLenght);
                passGenerator(passLenght);
                outFile<<password[i];
            }
            outFile << endl;
        }
        outFile.close();

        cout << "Passwords successfully generated and written to file " << filename << "" << endl;
    }

    void passGenerator(int passLenght)
    {
        password = new char[passLenght];

        for (int i = 0; i < numOfNumbers; ++i) {
            password[i] = char(rand() % 10 + 48);
        }
        for (int i = numOfNumbers; i < numOfNumbers + numOfBigChars; ++i) {
            password[i] = char(rand() % 26 + 65);
        }
        for (int i = numOfNumbers + numOfBigChars; i < passLenght; ++i) {
            password[i] = char(rand() % 26 + 97);
        }
         std::random_shuffle(password, password + passLenght);
    }

    void numOfChars(int passLenght)
    {
        numOfSmallChars = rand() % passLenght;
        int charRandEnd = passLenght - numOfSmallChars;
        numOfBigChars = rand() % charRandEnd;
        numOfNumbers = passLenght - numOfSmallChars - numOfBigChars;
    }

private:
    int numOfSmallChars;
    int numOfBigChars;
    int numOfNumbers;
    char*password;
};

int main()
{
    srand(time(NULL));
    PassGen * pass = new PassGen;
    pass->displayMessage();
    return 0;
}

 

Softina generates any number of complex passwords of any length and writes them to a text file. Use for health.