Hi
I am generating 256 bit key for AES. I am using the following implementation
-
void fill_random_key(char *key)
-
{
-
/* Length of the key */
-
unsigned short int length = KEYLENGTH;
-
-
/* Seed number for rand() */
-
srand((unsigned int) time(0) + getpid());
-
int i=0;
-
-
while(length--) {
-
key[i]=rand() % 127;
-
srand(rand());
-
-
i++;
-
-
}
-
}
-
-
void get_keys_for_files(char **files)
-
{
-
extern int num_of_files;
-
char (*curr_file)[20]=files;
-
-
char key[32];
-
int i=0;
-
FILE *fp;
-
char key_file[1024];
-
for(i=0;i<num_of_files;i++)
-
{
-
-
strcpy(key_file,cwd);
-
strcat(key_file,"\\keys\\");
-
strcat(key_file,curr_file);
-
fill_random_key(key);
-
fp=fopen(key_file,"w");
-
fputs(key,fp);
-
curr_file++;
-
fclose(fp);
-
sleep(1000);
-
}
-
}
-
-
The problem with this code is that if I remove sleep it will give same keys for almost all files. But I do not want to deter performance of my module due to this. Can somebody let me know better implementation? rand_s() is not available. I am working on code blocks : mingw.
Thanks for your help