-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.c
47 lines (46 loc) · 1.18 KB
/
keygen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/******************************************************************************
* keygen.c
*
* Author: Greg Mankes
* Generates a key of specified length given over command line
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/******************************************************************************
* int main(int, char *)
*
* Main method
* args: command line arguments
*******************************************************************************/
int main(int argc, char * argv[]){
// check the number of args
if(argc != 2){
fprintf(stderr, "Incorrect number of arguments\nUsage: keygen <keylength>");
exit(1);
}
// get the key length
int key_length = atoi(argv[1]);
// seed random number generator
srand(time(0));
// set up loop var and key
int i = 0;
int key;
// begin generating keys
for(; i < key_length; i++){
// get a random number
key = rand() % 27;
// if the number is not 26, mapp it to a char
if(key != 26){
printf("%c", 'A'+(char)key);
}
else{
// if it is 26, it is a space
printf(" ");
}
}
// print a newline
printf("\n");
return 0;
}