How to generate a random password in C

2 Answers

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

void generatePassword(int password_length, char password[]) {
    srand(time(NULL));
    int i = 0;
    
    int tmp = password_length;
    while (tmp--) {
        password[i++] = rand() %  (126 - 33 + 1) + 33;
    }
    tmp = password_length;
}

int main(void) {
    char password[11] = "";
    
    generatePassword(10, password);
    
    puts(password);
 
    return 0;
}
  
  
  
/*
run :
  
8?*>SrV@4=

*/

 



answered Dec 21, 2024 by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

char* generatePassword(int password_length) {
    const char* charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!$()";
    char* password = malloc(password_length + 1);
    int charset_len = strlen(charset);
    
    for (int i = 0; i < password_length; i++) {
        password[i] = charset[rand() % charset_len];
    }
    
    password[password_length] = '\0';
    
    return password;
}

int main() {
    srand(time(NULL));
    
    char* password = generatePassword(10);
    
    printf("%s\n", password);
    
    free(password);
    
    return 0;
}

  
  
  
/*
run :
  
P8Ez5lZ)SN

*/

 



answered Dec 22, 2024 by avibootz

Related questions

3 answers 166 views
166 views asked Dec 22, 2024 by avibootz
1 answer 108 views
1 answer 108 views
1 answer 96 views
1 answer 100 views
100 views asked Dec 22, 2024 by avibootz
1 answer 104 views
104 views asked Dec 21, 2024 by avibootz
1 answer 102 views
...