sobota, 8 maja 2021

Password generator (C)

 #include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <string.h>


char char_Sp[25]={'~','!','@','#','$','%','^','&','*','(',')','-','+','>','{','}',

':','|','<','?','`','[',']',';',',','.'};


char Numbers[10];

char LowL[26];

char UpperL[26];


void init_Tab()

{

char i1,i2;

int j;

j=0;

for(i1='0';i1<'9'+1;i1++)

{

Numbers[j]=i1;

++j;

}

j=0;

for(i1='a',i2='A';i1<'z'+1;i1++,i2++)

{

LowL[j]=i1;

UpperL[j]=i2;

++j;

}

 

}

char write_Char()

{

int i,j;

char c;

i=rand()%4;

switch(i)

{

case 0:

j=rand()%26;

c=char_Sp[j];

break;

case 1:

j=rand()%10;

c=Numbers[j];

break;

case 2:

j=rand()%26;

c=LowL[j];

break;

case 3:

j=rand()%26;

c=UpperL[j];

break;

default:

break;

}

return c;

}


int main(int argc, char **argv)

{

srand(time(NULL));

init_Tab();

char file_Name[20];

char **passwd,res;

FILE *my_FILE;

int pas,chr,i,j;

printf("How many passwdords ? : ");

scanf("%d", &pas);

printf("How many characters ? : ");

scanf("%d", &chr);

printf("File name (max 20) ? : ");

scanf("%s",file_Name);

passwd=(char**)malloc(pas*sizeof(char*));

for(i=0;i<pas;i++)

{

passwd[i]=malloc(chr*sizeof(char*));

}

for(i=0;i<pas;i++)

{

for(j=0;j<chr;j++)

{

res=write_Char();

passwd[i][j]=res;

}

}

my_FILE=fopen(file_Name,"w");

for(i=0;i<pas;i++)

{

fprintf(my_FILE,"%s \n",passwd[i]);

}

fclose(my_FILE);

for(i=0;i<pas;i++)

free(passwd[i]);

free(passwd);

return 0;

}