19+ C Password Generator Examples

This post contains a total of 19 Hand-Picked C Password Generator examples with Source Code. All these password generators are made using C Programming language.

You can use the source code of these examples with credits to the original owner.

Related Posts

C Password Generator Examples

1. By Daniel Fragomeli

Made by Daniel Fragomeli. A simple C Password generator program that generates a 11 digit long random password. Source

)U(:R3dlg`F}ag,[t5mpoUW2VKq5Y6jR
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 16
char
    a[]="abcdefghijklmnopqrstuvwxyz",
    b[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int
    i,c[]={1,2,3,4,5,6,7,8,9,0},d;

void passgen() //without symbols
{
    for(i=0;i<N;i++){ 
        switch(rand()%3){
        case 0:
            printf("%c",a[rand()%26]);
            break;
        case 1:
            printf("%c",b[rand()%26]);
            break;
        case 2:
            printf("%d",c[rand()%10]);
            break;
        }  
    }
    return;
}

void pass_sym(){ //pass with symbols
    for(i=0;i<N;i++){
        d=33+rand()%94;
        printf("%c",d);
    }
    return;
}

int main()
{
    srand(time(NULL));
    pass_sym();
    passgen();
    return 0;
}

2. By D Shaw

Made by D Shaw. You can get a random password of the length you entered. Source

10
&RB7H84:ky
#include <stdio.h>
#include <stdlib.h>
void generator( int N )
{
	char letters[]={"abcdefghijklmnopqrstuvwxyz"};
	char LETTERS[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
	char numbers[]={"1234567890"};
	char symbols[]={"_-+*:!?&"};
	char password;
	int n;
	for(int i = 1;i<=N;++i)
	{
		n = rand()%4;
		if(n == 0)
			password = letters[ rand()%26 ];
		else if(n == 1)
			password = LETTERS[ rand()%26 ];
		else if(n == 2)
			password = numbers[ rand()%11 ];
		else
			password = symbols[ rand()%9 ];
		printf("%c",password);
	}
}
int main()
{
	int N;
	scanf("%d",&N);
	generator(N);
	return 0;
}

3. By Vamsi

Made by Vamsi. This C program can generate multiple passwords of same length, first enter length after that the numbers of password you need. Source

ENTER REQUIRED LENGTH OF PASSWORD: 8
ENTER NUMBER OF PASSWORDS YOU NEED: 10
nmCvmqrw
ZW\k_phj
pftGV`tB
r[EXlXuj
AwvbqxK]
CgVdBNDx
NJWArhiZ
rFxeK_ls
L[luZviZ
ggDx`TZe
#include<time.h>

#include<stdio.h>

#include<stdlib.h>

int main(void) {
  int length;
  int num;
  int temp;
  printf("ENTER REQUIRED LENGTH OF PASSWORD:");
  scanf("%d", & length);
  printf("ENTER NUMBER OF PASSWORDS YOU NEED:");
  scanf("%d", & num);
  srand((unsigned int) time(0) + getpid());
  while (num--) {
    printf("\n");
    temp = length;
    while (temp--) {
      putchar(rand() % 56 + 65);
      srand(rand());
    }
    temp = length;
  }
  printf("\n");
}

4. By Swapnil More

Made by Swapnil More. Enter the length of the password that you want to generate. The password will be generated with a mix of alphabets, numbers and symbols. Source


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    int i,n,a,b;
    char arr[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
    b=strlen(arr);
    srand(time(0));
    printf("Enter strength of password: ");
    scanf("%d",&n);
    printf("%d\n",n);
    printf("The %d digit password is: ",n);
    for(i=0;i<n;i++){
        a=rand()%b;
        printf("%c",arr[a]);
    }
    return 0;
}

5. By Hasan Vurucu

Made by Hasan Vurucu. First enter what you want to do, whether you want to generate a password or validate it. After that enter the length of the password you want to generate. Source

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<time.h>

int main()
{
	srand(time(NULL));
	int methodNumb, i;
	printf("Choose the password method you want to use:\n");
	while(1)
	{
		//Choosing the password method
		printf("1: Random Generator | 2: Password Validator\n");
		scanf("%d", &methodNumb);
		if(methodNumb == 1 || methodNumb == 2)
		{
			break;
		}
		else
		{
			("Unvalid choice!\nChoose the method again:\n");
		}
	}
	if(methodNumb == 1)
	{
		//random password generator by length
		int length;
		while(1)
		{
			//Asking for length
			printf("Enter the length for your password(Min 8, Max 32): \n");
			scanf("%d", &length);
			if(length <= 32 && length >= 8)
			{
				break;
			}
			else
			{
				printf("unvalid length!\n");
			}
		}
		int password[length];
		
		for(i = 0; i < length; i++)
		{
			password[i] = rand() % 93 + 34; //generates a random number between 33 and 126 (33 & 126 included.)
		}
		
		printf("Generated password is: ");
		//Printing integers as chars.
		for(i = 0; i < length; i++)
		{
			printf("%c", password[i]);
		}
		
	}
	else if(methodNumb == 2)
	{
		//User defined, tells the strength (use string functions!)
		int strength = 0, i;
		char password[256];
		printf("Enter a password including at least a lowercase letter, an uppercase letter and a number.\nMin 8, Max 32 characters.\n");
		printf("Enter your new password: \n");
		while(strength < 3)
		{
			while(strength < 3)
			{
				fflush(stdin); //Clear the input buffer after reading the character
				gets(password); //User Input
				
				if(strlen(password) > 32 || strlen(password) < 8)
				{
					printf("Invalid length!\nEnter your new password again(Min 8, Max 32 characters): \n");
				}else
				{
					break;
				}
			}
			
			//looking for if a number exist.
			for(i = 0; i < strlen(password); i++)
			{
				if(isdigit(password[i]))
				{
					strength++;
					break;
				}
			}
			
			//looking for if a lowercase letter exist.
			for(i = 0; i < strlen(password); i++)
			{
				if(islower(password[i]))
				{
					strength++;
					break;
				}
			}
			
			//looking for if a uppercase letter exist.
			for(i = 0; i < strlen(password); i++)
			{
				if(isupper(password[i]))
				{
					strength++;
					break;
				}
			}
			
			if(strength >= 3)
			{
				printf("Nice one! Your password has been set!\n");
				printf("Your new password is %s", password);
			}else
			{
				strength = 0; //reset strength variable
				password[0] = 0; //reset the string with null terminator
				printf("Your password is not strong enough!");
				printf("Enter a password again: \n");
			}
		}
	}
	return 0;
}

6. By AsterisK

Made by AsterisK. Enter password length you want between 8 -32. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
	int len,i,asc= 65, rand_num=0,s_len;
	char paswd= '\0';
	char search[73] = "[email protected]#$%^&*?AQWERTYUIOPSDFGHJKLMNBVCXZ";
	printf("\nthe pasword length should be between 8-32");
	printf("\nenter the length of your password: ");
	scanf("%d",&len);
	srand(time(NULL));
	s_len = strlen(search);
	//rand_num = rand()%s_len;
	if(len<8 || len > 32){
	printf("\nplease kindly follow the instruction given above, thank you");
}
	else{
	printf("\nyour password is:");
	for(i=0;i<len;i++){
		rand_num = rand()%s_len;
		paswd = search[rand_num];
		printf("%c",paswd);
	}
	
}
	
	return 0;
}

7. By Pringle

Made by Pringle. Enter a password length to generate, must be above 5. Source

#include <stdlib.h>
#include <stdio.h>
#include<time.h>
int main(){
 int i,password,len;
 srand((unsigned int)time(NULL));
 printf("Enter Password Length and must be greater than 5\n");
 scanf("%d",&len);
 if(len>=5){
   for(i=0;i<len;i++){
     int k=rand()%128;
       if((k>=48&&k<=57)||(k>=65&&k<=90)||(k>=97&&k<=122)||(k>=35&&k<=37)||k==64){
          printf("%c",k);
      }
       else{
          i--;
         }
    }
    printf("\n");
   }
 else{
   printf("Length should be greater than 5\n");
 }
    return(0);

}
 

8. By zhoolf

Made by zhoolf. Simply enter a decimal number as the length of the password. Source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// max and min length for the password
#define PASSWORD_MAX_LEN 200
#define PASSWORD_MIN_LEN   1

// ASCII characters not allowed for a password
#define ASCII_34 34    // double accent
#define ASCII_39 39    // simple accent
#define ASCII_60 60    // less-than sign
#define ASCII_62 62    // greater-than sign
#define ASCII_91 91    // open bracket
#define ASCII_92 92    // reverse slash
#define ASCII_93 93    // close bracket
#define ASCII_123 123  // open keys
#define ASCII_124 124  // vertical slash
#define ASCII_125 125  // close keys

// ASCII characters that are not allowed as the first character of the password
#define ASCII_45 45 // dash
#define ASCII_46 46 // dot

// first and last printable ASCII character
#define ASCII_FIRST 33
#define ASCII_LAST 126

// function prototypes
unsigned int validatePasswordLength (unsigned int);
int validatePasswordFormat (unsigned int, unsigned int);
void generatePassword (unsigned int);


int main()
{
    srand((unsigned int)time(NULL));

    unsigned int len = 0;
    scanf("%u", &len);

    generatePassword(len);

    return 0;
}


unsigned int
validatePasswordLength (unsigned int len)
{
    if (len < PASSWORD_MIN_LEN || len > PASSWORD_MAX_LEN) {
        printf("Error: invalid value for length: ");
        (len<1) ? printf("password too short") : printf("password too long");
        return -1;
    }
    
    return 0;
}

int
validatePasswordFormat (unsigned int asciichar, unsigned int index)
{
    if (index == 0) {
        if ((asciichar == ASCII_45) || (asciichar == ASCII_46))
            return -1;
    }
    switch (asciichar) {
        case ASCII_34: case ASCII_39: case ASCII_60:  case ASCII_62:  case ASCII_91:
        case ASCII_92: case ASCII_93: case ASCII_123: case ASCII_124: case ASCII_125:
            return -1;
    }
    
    return 0;
}

void
generatePassword (unsigned int length)
{
    if (validatePasswordLength(length) == -1)
        return;

    char *passwrd = calloc(length, sizeof(char));
    unsigned int idx, asciichar;

    for (idx=0; idx < length; idx++) {
        do    asciichar = (rand()%(ASCII_LAST-ASCII_FIRST+1))+ASCII_FIRST;
        while (validatePasswordFormat(asciichar, idx) == -1);

        *(passwrd + idx) = asciichar;
    }

    puts(passwrd);
    free(passwrd);
}

9. By Sunil Kunwar

Made by Sunil Kunwar. Source

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n,i,r;
    printf("enter length of password:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        r=rand()%123;
        if(r<35)
        {
            r=r+35;
        }
        if(((r>=39)&&(r<=47))||((r>=58)&&(r<=62))||((r>=91)&&(r<=96)))
        {
            --i;
            continue;
        }
        else
        {
            printf("%c",r);
        }
     }
    return 0;
}

10. By Rachid Elbair

Made by Rachid Elbair. Source

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {

int i;

char password[10];

 char alphabets[]="[email protected]#$&";
 
srand((unsigned) time(0));

for(i=0;i<10;i++){

    password[i]=alphabets[rand()%59];
}
printf("%s\n",password);

    return 0;
}

11. By Joona Granlund

Made by Joona Granlund. Enter password length to generate a random password containing alphabets, symbols and numbers. Source

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main() {
	int len, a_len, ra, lol;
	scanf("%d", &len);
	char all[138] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789012345678901234567890{([])}.&/%%@!?{([])}.&/%%@!?{([])}.&/%%@!?{([])}.&/%%@!?";
	a_len = strlen(all);
	srand(time(NULL));
	char password;
	for(int i=0; i<len; i++){
		password = all[rand()%a_len];
		printf("%c", password);
		
	}
}

12. By SURJEET KUMAR

Made by SURJEET KUMAR. The program generates a random 10 digits password. Source

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int i,len;
	srand((unsigned int)time(NULL));
	printf("\n\n\n\t\t\t------- PROGRAM TO GENERATE THE PASSWORD OF LENGHT 8 ----- ");
printf("\n");
	len=10;
	if(len>=5&&len<=20)
	{
		for(i=0;i<len;i++)
		{
			int k=rand()%128;
			if((k>=48&&k<=57)||(k>=65&&k<=90)||(k>=97&&k<=122)||(k>=35&&k<=37)||k==64)
			{
				printf("%c",k);
			}
			else
			{
				i--;
			}
		}
		printf("\n");
	}
	else
	{
		printf("\n\t\t\t LENGTH SHOULD BE GREATER THAN 5 and equal or less than 20 !!! ");
		
	}
	return 0;
}

13. By ali ghanbari

Made by ali ghanbari. Enter a password length to generate a random password. Source

#include <stdio.h>
#include <time.h>

int main() {
    srand(time(NULL));
    const int N;
    scanf("%d",&N);
    char p[N];
    for(int i=0;i<N;i++){
        p[i] = (rand() % 60)+50;
    }
    printf("%s",p);
    return 0;
}

14. By Atiksh Genius

Made by Atiksh Genius. Input password length to generate. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    srand(time(0));
    char rand_char[72] = "[email protected]#$%^&*?AQWERTYUIOPSDFGHJKLMNBVCXZ";
    int input;
    scanf("%d", &input);
    while(input){
        printf("%c", rand_char[rand()%71]);
        input--;
    }
    return 0;
}

15. By Dylan Heslop

Made by Dylan Heslop. The program generates a random password that is random in length. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int length, count, num_count=0, alpha_count=0, sym_count=0;
    int rand_num;
    char c;
    

    srand(time(NULL));
    length = 8 + rand() % 5;//randomly choose length of password 8-12

   printf("YOUR PASSWORD IS: ");

    for(count=0; count < length; count++)
    {
        //chose a random number each number corresponds to a character
        rand_num = 1 + rand() % 6;

            if(rand_num == 1)//select symbol 1/6 probability
            {
                if(rand() % 2 == 1)//select different sets of symbol
                {
                    c = 33 + rand() % 15;
                }else
                {
                    c = 60 + rand() % 5;
                }
                sym_count++;

                }else if(rand_num == 3 || rand_num ==5)//select number 2/6 probability
                {
                    c =  48 + rand() % 10;
                    num_count++;
                }else //3/6 probability (letters)
                {
                    if(rand() % 2 ==1)
                    {//select upper case letter
                        c = 65 + rand() % 27;
                    }else //    
                    {//select lower case letter
                        c =  97 + rand() % 26;
                    }
                    alpha_count++;

                }
        printf("%c", c);
    }//endfor

    printf("\nNumber Count: %d\n Alpha Count %d\n Symbol Count %d\n", num_count, alpha_count, sym_count);
    return 0;
}

16. By rudolph flash

Made by rudolph flash. Enter an integer as the password length you want. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

char* generate(int n) {
    char* password = malloc((n + 1) * sizeof(*password));
    
    if (!password)
        exit(EXIT_FAILURE);
        
    srand((size_t)time(NULL));
    
    for (int i = 0; i < n; i++)
        password[i] = (char)(rand() % 94 + 33);
    password[n] = '\0';
    
    return password;
}

int main() {
    char* password;
    int n;
    
    if (!scanf("%i", &n) || n < 0)
        exit(EXIT_FAILURE);
        
    password = generate(n);
    
    if (password) {
        printf("Password: %s", password);
        free(password);
        password = NULL;
    }
    return 0;
}

17. By HonFu

Made by HonFu. A Recursive C Password Generator. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void create_pw(
        char* pw, int size, int index) {
    if (index==size) {
        pw[index] = '\0';
        return;
    }
    else {
        create_pw(pw, size, index+1);
        do 
            pw[index] = rand()%94+33;
        while(pw[index]=='\\'||pw[index]==96);
    }
}

int main() {
    int size;
    printf("Password generator\n");
    printf("------------------\n\n");
    printf("Length (8-32 letters)? ");
    scanf("%d", &size);
    printf("%d", size);
    if (size<8||size>32) {
        printf("\n\nUnfitting size!");
        return 1;
    }
    char password[size+1];
    srand((unsigned)time(NULL));
    create_pw(password, size, 0);
    printf("\n\nYour password:\n\n");
    printf("%s", password);
    return 0;
}

18. By Harshana Chathuranga

Made by Harshana Chathuranga. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int a;
    int b;
    int c;
    int d;
    int e;
    int f = 7;
    printf("\n\n");
    printf("Please enter the length of the password that you want :\n");
    scanf("%d",&e);
    printf ("\n\nLength you entered is : %d\n",e)
;
    
    if (e<8 || e>12)
    {
        printf("\n\nLength must be between 8 - 12");
        //main();
    }
    else
    {
    printf("\n\n");
    for(int i = 0;i<=e;i++)
    {

    srand(time(&c));
    a = c%100;
    b = c%10;
    d = a + b + f;

    if(d>=127)
    {
        d = d - 70;
    }
    if(d>=10 && d<=35)
    {
        d = d + 77;
    }
    printf("%c",d);
    f = f + 10;
    }
    printf("\n\nThank You..!!\n");
}
    return 0;
}

19. By Mostafa Ehab

Made by Mostafa Ehab. Enter password length as input, this program will generate a random password by using a set of characters. The length must be between 8 and 32 characters, Numbers, English alphabet (uppercase and lowercase) and symbols (ASCII) are allowed. Spaces, invisible ASCII and non-ASCII characters are not. Source

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
	
	time_t t;
	srand(time(&t)); 
	
	int len;
	scanf("%i", &len);
	
	if (len < 8 || len > 32)
	{
		printf("Invalid length\n");
		return 1;
	}
	
	printf("Your password: ");
	
	for(int i = 0; i < len; i++)
	{
		char r = (rand() % 93) + 33; 
		 
		 printf("%c", r);
	}
	return 0;
}

20. By Nicolas Kaiser

Made by Nicolas Kaiser. Enter password length between 8 -32 to generate a random password. Source

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

const char cset[] = "[email protected]#$/^&*()-\'\":;,?+=%_<>{}[]`~\\|.";

int main(int argc, char *argv[]) {
    int i, n;
    scanf("%d", &n);
    if (n < 8 || n > 32) {
        printf("Input %d is not between 8 and 32.", n);
        return 0;
    }

    srand(time(NULL));
    for (int i = 0; i < n; i++) {
        putchar(cset[rand() % sizeof(cset)]);
    }
    putchar('\n');
    return 0;
}