Thread: Encryption Key Generator

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    28

    Encryption Key Generator

    I wrote the following program to generate a random key file for use in an encryption program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
         printf("Key noise collector");
         if (argc < 2) {
              printf("\nGenerates n bytes of noise, where n = # keystrokes you give it");
              printf("\nUSAGE: keynoise [OUTPUT]");
              return 0;
         };
    
         printf("\nAccessing output...");
         FILE * output = fopen(argv[1],"wb");
         if (NULL == output) {
              printf("\nFile could not be opened for writing");
              return 0;
         };
    
         printf("\nPOUND THEM KEYS\nPress ESC when done");
         unsigned int sum;
         unsigned int size = 0;
         char k = getch();
         while(k != 27) {
              sum += k;
              if (kbhit()) {
                   k = getch();
                   size++;
                   fputc((unsigned char) (sum % 256), output);
              };
         };
         fclose(output);
         printf("\n%u bytes generated",size);
         return 0;
    }
    My question is, can anyone see a problem with the key generation system, or is it sufficiently random to guarantee a strong key?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well it's not really all that random - as far as I can tell you're simply performing complex operations on user input and outputting it as the key. The complex operations-thing is a good idea - but you may want to start with the srand() and rand() functions. There are tutorials that teach you how to use those functions to generate results that are as random as possible.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    28
    The idea is that there are two sources of randomness here:
    1. The stuff the user types.
    2. How many cycles elapse between keystrokes.

    Look at the while(k != 27) loop. It adds the value of the last key pressed to the sum over and over until another key is pressed, at which point we output (sum % 256) to the file as a character. The actual key pressed and the number of cycles between keypresses are both uncertain, which should create randomness.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Oh okay - your number 2 sounds pretty good - but I wouldn't rely too much on that number 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM