random password generator

This is a discussion on random password generator within the C Programming forums, part of the General Programming Boards category; Hi, I am new to C and I am trying to write a program that will take in regular easy ...

  1. #1
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26

    random password generator

    Hi, I am new to C and I am trying to write a program that will take in regular easy to remember passwords like 'JohnDoe' and encrypt them with random characters. This is what I have so far:
    //passGen.c
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
        int length = 8;
        int r,i;
        char c;
        srand((unsigned int)time(0)); //Seed number for rand()
        for(i=0;i<length; i++) {
            r = rand() + 33;
            c = (char)r;
            printf("%c",c);
        }
    }
    To run it in Linux/Unix do ./a.out or a.out passGen JohnDoe
    The program works just the way I want it to, except I would like to narrow down the types of characters it uses. How would I modify it to only use 'normal' English letters (both upper and lower case,) numbers and only the additional characters ^, #, $ and @ for encryption? Also, I want to keep the integer length the same.
    Last edited by cDev; 07-06-2006 at 08:12 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    How would I modify it to only use 'normal' English letters (both upper and lower case,) numbers and only the additional characters ^, #, $ and @ for encryption?
    You could build an array (or string) of all the "acceptable" characters. Then you could just use the random integer that you generate (limited to the size of the array/length of the string) to grab that offset instead of using the generated integer directly as a character.
    take in regular easy to remember passwords like 'JohnDoe'
    It doesn't look like your program "takes in" anything at all. It's just spitting out 8 pseudorandom characters.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Quote Originally Posted by itsme86
    It doesn't look like your program "takes in" anything at all. It's just spitting out 8 pseudorandom characters.
    Whoops, you're absolutely right. What I really want this program to be able to do is to take in a given password and spit out an encrypted version, but in such a way that it produces the same result each time. In other words, I would like it to bind a random character to each character forthe encrypted output. I will work on this and post back what I come up with...

  4. #4
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Ok, I think I have fixed the character issue;

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void) {
        int length = 8;
        int r,i;
        char c;
        char a[66];
        
        for (i = 0; i < 26, i++) {
        	a[i] = 'a' + i;
        	a[i+26] = 'A' + i;
        }
        
        for (i = 0; i < 10; i++) {
        	a[i+52] = '0' + i;
        }
        
        a[62] = '^';
        a[63] = '$';
        a[64] = '@';
        a[65] = '#';
    
        srand((unsigned int)time(0)); //Seed number for rand()
        for(i=0;i<length; i++) {
            r = rand() % 66;
            c = a[r];
            printf("%c",c);
        }
    }
    Now, if anyone has any ideas of what would be the best way to make this input and output more specifically such that it would take in an unencrypted password and encrypt it the same way each time, that would be great. Any help/ideas are most appreciated.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Personally, I wouldn't use rand with an uncontrolled seed. I would probably just do a lot of work on the input string itself.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You might want to try using isalpha() or islower() and isdigit() or isalnum() in <ctype.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Even something really simple as swapping the nibble of the character and using that for the index works pretty well. I wouldn't trust it for anything serious, but it looks encrypted
    Code:
    itsme@itsme:~/C$ cat encrypt.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int swapnib(unsigned char ch)
    {
      int swapped;
    
      swapped = ((ch & 0xF) << 4) | (ch >> 4);
    
      return swapped;
    }
    
    int main(void)
    {
      char alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789^$@#";
      char input[] = "itsme86";
      char *output;
      int i;
      size_t len = strlen(input);
    
      output = malloc(len + 1);
    
      for(i = 0;i < len;++i)
        output[i] = alpha[swapnib(input[i]) % 66];
      output[i] = '\0';
    
      puts(output);
      free(output);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./encrypt
    sf3qu#H
    itsme@itsme:~/C$
    Last edited by itsme86; 07-06-2006 at 03:00 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    Thanks dwks and itsme86, great ideas. itsme86, I like what you came up with. I am going to play around with this some more. I know I could make this more sophisticated, but this is a great start, and is really helping me to learn C.

  9. #9
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    itsme86 --
    I think swapping the nibble is actually pretty effective, since in effect I would think you would always end up with a random character. I was wondering how easy it would be to modify this program such that it takes an input from the user at the console?
    i.e.
    Code:
    itsme@itsme:~/C$ ./encrypt itsme86
    sf3qu#H

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Really, the encryption I used is feeble at best. Each character you input would end up with the same character for output, not to mention other shortcomings. To get around that you could do various thing such as seeding the encryption with the length of the input string and using an accumulator:
    Code:
    itsme@itsme:~/C$ cat encrypt.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int hash(unsigned char ch)
    {
      return (int)(((ch & 0xF) << 4) + ((ch & 0xF0) >> 4));
    }
    
    int main(int argc, char **argv)
    {
      char alpha[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789^$@#";
      char input[BUFSIZ], *output;
      int i;
      unsigned int val, acc;
      size_t len;
    
      if(argc != 2)
      {
        char *p;
    
        printf("Enter string to encrypt: ");
        fflush(stdout);
        fgets(input, sizeof(input), stdin);
        if((p = strchr(input, '\n')))
          *p = '\0';
      }
      else
      {
        strncpy(input, argv[1], sizeof(input));
        input[sizeof(input) - 1] = '\0';
      }
    
      len = strlen(input);
      output = malloc(len + 1);
    
      for(i = 0, acc = len;i < len;++i)
      {
        val = hash(input[i]);
        output[i] = alpha[(val + hash((unsigned char)(acc & 0xFF))) % 66];
        acc += val;
      }
      output[i] = '\0';
    
      puts(output);
      free(output);
    
      return EXIT_SUCCESS;
    }
    Code:
    itsme@itsme:~/C$ ./encrypt
    Enter string to encrypt: hello world
    UT8uLuLCW5E
    itsme@itsme:~/C$ ./encrypt "hello world\!"
    mlA0d0d8oq^j$
    itsme@itsme:~/C$
    Note that I don't have any real experience in encryption. I'm sure others here could point out other serious defects in the methods used.
    Last edited by itsme86; 07-07-2006 at 09:37 AM.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User cDev's Avatar
    Join Date
    Jun 2006
    Posts
    26
    itsme86 --
    I knew hash coding would be important for a program such as this, but was unsure of how best to utilize it. I think you provided another very good solution. I see what you mean about the pitfalls of swapping the nibble of each character. As for stronger encryption...the only thing that comes to mind is a program that would change every single bit per character randomly. As for how difficult this would be to program, and how fast it would run I do not know. Originally I thought this generator would be as simple as 10 lines; just goes to show how new I am to C!

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Originally I thought this generator would be as simple as 10 lines
    Well, it could be. Half of the program is just getting the input string :P
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  2. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  3. written command line password generator
    By lepricaun in forum C Programming
    Replies: 15
    Last Post: 08-17-2004, 08:42 PM
  4. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 07:10 AM
  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

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