Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <time.h>
- #include <cs50.h>
- /*
- 1. Create function that creates random key.
- 2. memset(arr, 0, sizeof(arr));
- */
- char random_key[6];
- char plain_text[50];
- int keychartoint(char letter);
- char *encryption(char text[], char *key, char mode);
- char *generate_key();
- char correct_account_password[50];
- bool check_username(string username, FILE *account_file);
- bool check_password(string password, FILE *account_file);
- int main(void)
- {
- FILE *key = fopen("key.txt", "a");
- fclose(key);
- key = fopen("key.txt", "r");
- fgets(random_key, 6, key);
- if(strcmp(random_key, "") == 0)
- {
- strcpy(random_key, generate_key());
- }
- fclose(key);
- key = fopen("key.txt", "w");
- fputs(random_key, key);
- fclose(key);
- printf("Welcome to LocalPasswordManager! Do you have an account? Enter y for yes and n for no: ");
- string has_account = get_string();
- while(!(strcmp(has_account, "y") == 0 || strcmp(has_account, "n") == 0))
- {
- printf("Please enter either y for yes and n for no: ");
- has_account = get_string();
- }
- if(strcmp(has_account, "y") == 0)
- {
- FILE *accounts = fopen("accounts.txt", "r");
- printf("Please enter your username: ");
- string account_username = get_string();
- account_username = encryption(account_username, random_key, 'e');
- bool correct_username = false;
- while(!correct_username)//while the username is not correct
- {
- if (check_username(account_username, accounts))
- {
- correct_username = true;
- printf("Welcome\n");
- }
- else
- {
- printf("Please enter a valid username: ");
- account_username = get_string();
- account_username = encryption(account_username, random_key, 'e');
- fseek(accounts, 0, SEEK_SET);
- }
- }
- account_username = encryption(account_username, random_key, 'd');
- printf("%s, please enter your password: ", account_username);
- string account_password = get_string();
- account_password = encryption(account_password, random_key, 'e');
- char account_password_string[50];// <----------------------------// whY is this here
- strcpy(account_password_string, account_password);
- bool correct_password = false;
- while(!correct_password)
- {
- if(check_password(account_password_string, accounts))
- {
- correct_password = true;
- }
- else
- {
- printf("length of correct password: %lu\n", strlen(correct_account_password));
- printf("Please enter the correct password: ");
- printf("%s, %s, %s\n", correct_account_password, account_password_string, account_password);
- account_password = get_string();
- account_password = encryption(account_password, random_key, 'e');
- while(strcmp(account_password, correct_account_password) != 0)
- {
- printf("Please enter the correct password: ");
- account_password = get_string();
- account_password = encryption(account_password, random_key, 'e');
- }
- correct_password = true;
- }
- }
- account_password = encryption(account_password, random_key, 'd');
- printf("Welcome!\n");
- fclose(accounts);
- //open account of username.txt
- //enter accounts
- //enter name of account
- //enter username for account
- //enter password
- }
- else
- {
- FILE *accounts = fopen("accounts.txt", "a");
- printf("Please create a new username: ");
- // add this to create the suer file with user's name
- char account_username[50];
- FILE *fp;
- scanf("%s",account_username);
- fp=fopen(account_name,"w");
- fclose(fp);
- //string account_username = get_string();
- account_username = encryption(account_username, random_key, 'e');
- fputs(account_username, accounts);
- fputs("\t", accounts);
- account_username = encryption(account_username, random_key, 'd');
- printf("Please create a password: ");
- string account_password = get_string();
- char account_username_string[50];
- strcpy(account_username_string, account_username);
- account_password = encryption(account_password, random_key, 'e');
- fputs(account_password, accounts);
- fputs("\n", accounts);
- account_password = encryption(account_password, random_key, 'd');
- fclose(accounts);
- printf("Welcome, %s!\n", account_username_string);
- }
- //while(key != quit)
- //{
- //display options for resetting the password, accessing a specific website‘s username
- //and password, adding a website, deleting a website, or changing website information.
- //if the user chooses to add a website, add the website information to the end of the file.
- //if the user chooses to delete website information, then replace the information with
- //spaces in the file and remove the website from the list.
- //if the user chooses to access a website‘s information, then
- //find the information on the file and display it,
- //if the user chooses to reset the one-time password, then decrypt the information
- //and encrypt it using the new one-time password.
- //If the user chooses to change website information, then ask the user if they want to
- //change the name, the username, or the password, and then change the text in
- //the file appropriately
- //here, ask the user what he or she would like to so
- //}
- }
- int keychartoint(char letter)
- {
- int alpha_num = toupper(letter) - 65;
- return alpha_num;
- }
- char *encryption(char text[], char *key, char mode)
- {
- //MODES: e-encrypt, d-decrypt
- //char plain_text_array[50];
- //char *plain_text = " ";
- //char plain_text[50];
- strcpy(plain_text, text);
- if(mode == 'e'){
- }
- int key_char = 0;
- for (int text_char = 0; text_char < strlen(text); text_char++)
- {
- if (isalpha(text[text_char]))
- {
- int char_num = text[text_char];
- /*Different algorithms are used for upper and lower case
- characters because of their differences in the ASCII index.*/
- if (isupper(text[text_char]))
- {
- char_num -= 65;
- if(mode == 'e')
- {
- char_num = (char_num + keychartoint(key[key_char])) % 26;
- }
- else if(mode == 'd')
- {
- char_num = (char_num - keychartoint(key[key_char]) + 26) % 26;
- }
- char_num += 65;
- }
- else
- {
- char_num -= 97;
- if(mode == 'e')
- {
- char_num = (char_num + keychartoint(key[key_char])) % 26;
- }
- else if(mode == 'd')
- {
- char_num = (char_num - keychartoint(key[key_char]) + 26) % 26;
- }
- char_num += 97;
- }
- plain_text[text_char] = char_num;
- key_char++;
- /*This if statement returns back to the first character
- if all of the characters in the keyword have been used.*/
- if (key_char >= strlen(key))
- {
- key_char = 0;
- }
- }
- }
- return plain_text;
- }
- char *generate_key()
- {
- static char string_key[6];
- srand(time(NULL));
- int rand_num = (rand() % 26) + 65;
- for(int i = 0; i < 5; i++)
- {
- string_key[i] = rand_num;
- rand_num = (rand() % 26) + 65;
- }
- return string_key;
- }
- bool check_username(string username, FILE *account_file)
- {
- char buffer[50];
- char current_character = fgetc(account_file);
- int i = 0;
- while(current_character != EOF)//while the file has not ended
- {
- while(current_character != '\t')
- {
- buffer[i] = current_character;
- current_character = fgetc(account_file);
- i++;
- }
- if(strcmp(buffer, username) == 0)
- {
- return true;
- }
- else
- {
- while(current_character != '\n' && current_character != EOF)
- {
- current_character = fgetc(account_file);
- }
- }
- memset(buffer, 0, sizeof(buffer));
- i = 0;
- current_character = fgetc(account_file);
- }
- return false;
- }
- bool check_password(string password, FILE *account_file)
- {
- char buffer[50];
- char current_character = fgetc(account_file);
- int i = 0;
- while(current_character != '\n')
- {
- if(current_character == '\n')
- {
- break;
- }
- buffer[i] = current_character;
- current_character = fgetc(account_file);
- i++;
- }
- int j = 0;
- while(buffer[j] != '\0')
- {
- j++;
- }
- if(fgetc(account_file) != EOF)
- {
- buffer[j - 1] = 0;
- }
- if(strcmp(buffer, password) == 0)
- {
- return true;
- }
- else
- {
- strcpy(correct_account_password, buffer);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement