Thread: Password generator

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    40

    Question Password generator

    I would like to make myself a little simple app with 2 buttons only in c++. I know this would be a windows app but i want it in c++. I think I know how to make it for dos but for windows might be tricky part. Im pretty new to c++ and would like someone to guide me on good path for this one.

    This is what I want. Simple app that generates a password of 8 char inluding letters and numbers. must start with a letter. all small caps. I know how to do it with numbers with the rand command but its the letters part hehe. another thing is i wouldnt know how to mix them. meaning for it to generate a letter or number.

    2 butons. 1 to generate and the other to copy to clipboard. pretty simple app but the hard part for me is to include letters. if it was a number pasword only then i would only have to figure out the part of importing that into a windows kinda app instead of dos.

    Anyone?

    By the way this is for me to use at work not a school project or homework hehe. just to make this clear. im learning c++ for my pleasure and to make it maybe in a few years my living.

    Im not asking that someone makes me the app, I just want to make this my fiorst official app I would do and would like help to get it started if possible.

    thanks
    Last edited by MrJake; 02-20-2002 at 05:02 AM.
    cout << "Gotta love cpp\n";
    cout << "UIN: 825265\n";
    cout << "[email protected]\n;
    /* Hope I can know enough one
    day to make my very own notepad
    */

  2. #2
    Unregistered
    Guest
    Characters (numbers, letters, etc.) can be treated as numerical values. For example, if you say
    char c = 'G';
    cout << (int)c;
    you'll get 71 printed out. You can also declare an integer as
    int n = 'A';
    and n will now be 65.

    The most easiest way to accomplish what you try to do is to make a loop that goes on until randomized value is number or a letter (mind the case). This loop would, of course, be nested in another loop that assigns each cell in a string:

    Code:
    char pw[10], c;
    for(int i = 0;  i < 10;  i++)
    {
      for(c = 0;  isdigit(c) == 0;)
        c = '0'+(rand()%('z'+1-'0'));
      pw[i] = c;
    }
    for(c = 0; isalnu,(c) == 0
    goes through until c is a number/letter

    c = '0'+(rand()%('z'+1-'0'));
    c can now be anything between '0' and 'z'. '0' is the smallest from digits and letters what comes to numerical value and 'z' is the highest (we need to add 1 to 'z' to include 'z' itself and substract '0' because we already have it in the beginning of the statement - alternatively you could write: c = 48+rand()%75).

    Hope I wasn't too complicated :-)

  3. #3
    Unregistered
    Guest
    Replace that annoying yellow ball with ;)

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    I did a bit a code for this problem in an old project, but you'll have to modify it so it does 8.

    void RandomizePasswords(char Passwords[MAX_LEVEL][PASSWORD_LENGTH])
    {
    const int Letters=25, Numbers=9, LettersBase=97, NumbersBase=48;

    int i=0,Temp,Index=0;

    randomize();

    while(Index < MAX_LEVEL)
    {
    i=0; //Reinitalise i for next password randomization
    for(i; i < PASSWORD_LENGTH-1; i++)
    {
    if(i <= 1)
    {
    Temp = LettersBase + random(Letters+1);
    Passwords[Index][i]= (char)Temp;
    }
    else
    {
    Temp = NumbersBase + random(Numbers+1);
    Passwords[Index][i]= (char)Temp;
    if(i==5) //Put end of string on
    Passwords[Index][i+1]='\0';
    }
    }
    Index++;
    }

  5. #5
    Unregistered
    Guest

    Post

    Here there are some functions I have made. Hope this helps too.

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <stdlib.h>
    
    /* Generates one random letter */
    char gen_letter(void)
    {
      char ch = (rand() % 26) + 'a';
    
      return ch;
    }
    
    /* Generates one alphanumeric character */
    char gen_alphanum(void)
    {
      char vAlpha[] = "0123456789abcdefghijklmnopqrstuvwxyz";
      char ch = vAlpha[ rand() % 36 ];
      
      return ch;
    }
    
    /* Use this to generate the password of length 'iSize' */
    void gen_pass(char *sPassWord, int iSize)
    {
       if (iSize < 1)
       {
         sPassWord[0] = '\0';
         return;
       }
      
       /* First must be a letter */
       sPassWord[0] = gen_letter();
    
       for (int i=1; i < iSize; i++)
         sPassWord[i] = gen_alphanum();
    
       /* Terminates the string */
       sPassWord[iSize] = '\0';
    }
    
    int main(void)
    {
        char password[9];
      
        /* Get a seed number */
        srand((unsigned int) (GetTickCount() / 100) );
    
        gen_pass(password, 8);
        cout << "\nPassword: '" << password;
    
        return 0;
    }
    As it was said: Letters can be treated as numbers (ASCII code).

    Use: rand() % Num
    to generate a random number between 0 and Num-1.

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88
    #include "iostream.h"
    #include "stdio.h"
    #include "stdlib.h" // rand()

    Code:
    int main()
    {
    	int i, c, d, num_chars, num_passwds;
    	static char *letters[] = { "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" };
    	char FileName[30];
    
    	cout << "\n How many Chars?     : ";
    	cin >> num_chars;
    
    	cout << " How many Passwords? : ";
    	cin >> num_passwds;
    
    	cout << "         Output File : ";
    	cin >> FileName;
    	
    	FILE *wordfile;
    	wordfile = fopen(FileName,"w");
    
    	d = 1;
    	fflush(wordfile);
    
    	while(d <= num_passwds)
    	{
    		d++;
    		c = 1;
    		
    		while(c <= num_chars)
    		{
    			c++;
    			i = rand() % 26;
    			fprintf(wordfile, "%s", letters[i]);
    		}
    		fputs("\n", wordfile);
    	}
    	
    	fclose(wordfile);
    
    	return 0;
    }

    Yes, i know.. this is some wierd'ed-out code(not clean looking). I didn't spend much time on it, and its very simple stuff.

    Heres an Idea.
    ----
    I just thought about this ...
    Instead of programming a "random" password generator(because there is already enough out there), why not program a "real" word generator? Heres how it could work.

    ----
    Pass an actual document on something familiar(maybe a real dictionary)through the program, then have it split up everyword and check for repeats of the same word ... this way, you would have real words, and not random characters. And if You know what your victim is into, what he or she likes, but you don't know what words to use, pass a document on whatever they like and you would have "real" words on that subject without ever knowing anything on it.

    Would go something like this
    ----
    - Get a file that talks about something
    - Split everyword up and write it to a file
    - Read-in the file that you wrote to, and check for repeats - if any repeats, take them out and re-write

    kinda interesting ...

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Thumbs up Cool password generator

    Nice idea, knave.

    What would the better word splitting method? Into syllables?
    Random characters passwords are more difficult to memorize, because they seems completely meanless.
    The person would enter a text or even a phrase... as you said.

    What about we code such a generator?
    Dharius

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    40
    im impressed but very confused. i like both methos. but the other one i prefer cause it gives an output in number and letters.

    Can someone explain it to me cause i cant figure it out. I want to make myself a nice little window box and incorporate that cod einto it somehow. that was the goal hehe. Another this is how can i make that the first char is a letter? I cant have a number as first letter. but this is the one i liek and need:
    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <stdlib.h>
    
    /* Generates one random letter */
    char gen_letter(void)
    {
      char ch = (rand() % 26) + 'a';
    
      return ch;
    }
    
    /* Generates one alphanumeric character */
    char gen_alphanum(void)
    {
      char vAlpha[] = "0123456789abcdefghijklmnopqrstuvwxyz";
      char ch = vAlpha[ rand() % 36 ];
      
      return ch;
    }
    
    /* Use this to generate the password of length 'iSize' */
    void gen_pass(char *sPassWord, int iSize)
    {
       if (iSize < 1)
       {
         sPassWord[0] = '\0';
         return;
       }
      
       /* First must be a letter */
       sPassWord[0] = gen_letter();
    
       for (int i=1; i < iSize; i++)
         sPassWord[i] = gen_alphanum();
    
       /* Terminates the string */
       sPassWord[iSize] = '\0';
    }
    
    int main(void)
    {
        char password[9];
      
        /* Get a seed number */
        srand((unsigned int) (GetTickCount() / 100) );
    
        gen_pass(password, 8);
        cout << "\nPassword: '" << password;
    
        return 0;
    }
    Its simple and is exactly what i need. I just need first letter to be a letter only. This is very sweet of you guys. But I more wanted to be helped heeh. you guys just made the whole app for me. But the good part is im confused and will hjave atleast a week to unscrable it to understand it with my book. Only if one of u has so much time to waste he feels like commenting it in huge details

    But thanks a lot i really apriciate and hope to be able one day to return favor and help u debug an app (liek in 10 years or so) hehe
    cout << "Gotta love cpp\n";
    cout << "UIN: 825265\n";
    cout << "[email protected]\n;
    /* Hope I can know enough one
    day to make my very own notepad
    */

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    Hey, MrJake.

    There is no mistery.
    Code:
    /* Generates one random letter */
    char gen_letter(void)
    {
      char ch = (rand() % 26) + 'a';
    
      return ch;
    }
    You said you know how to generates numbers with rand().
    rand() % 26 generates 0, 1, 2, 3, ..., 25 right?
    the + 'a' is the same of + 97, cause 97 is 'a' ASCII code.
    So (rand() % 26) + 'a', returns in ASCII: 97, 98, 99, ..., 122 that represents 'a', 'b', 'c', ..., 'z'
    Note that % 26 means 26 itens (letters).

    Code:
    /* Generates one alphanumeric character */
    char gen_alphanum(void)
    {
      char vAlpha[] = "0123456789abcdefghijklmnopqrstuvwxyz";
      char ch = vAlpha[ rand() % 36 ];
      
      return ch;
    }
    In this case, it generates an index for vAlpha, that has 36 itens (% 36).
    Note that ASCII codes of numbers and small caps letters are not on the same range.

    Good luck!
    Dharius

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    40
    Thanks man. Im gonna finish my book first then ill try to make myself a nice littel window app like i sais. 2 buttons. 1 to generate the pass, the other to copy to clip board. I have one my friend made in delphi but I dont like it hehe.

    Im a fussy man i guess. but your guys were great help. Now ill try to figure this all out. that means 1 month

    /*edit*/

    man i have to learn how to type hehe. when i reread my post it makes me feel like a 10 year old typed it.
    Last edited by MrJake; 02-25-2002 at 05:15 AM.
    cout << "Gotta love cpp\n";
    cout << "UIN: 825265\n";
    cout << "[email protected]\n;
    /* Hope I can know enough one
    day to make my very own notepad
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with password list generator
    By casper29 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 02:23 AM
  2. [Q]Hide Password
    By Yuri in forum C++ Programming
    Replies: 14
    Last Post: 03-02-2006, 03:42 AM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  5. Random Password Generator v1.0 - download it
    By GaPe in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-18-2002, 01:27 AM