Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <cs50.h>
  7.  
  8. /*
  9. 1. Create function that creates random key.
  10. 2. memset(arr, 0, sizeof(arr));
  11. */
  12.  
  13. char random_key[6];
  14. char plain_text[50];
  15. int keychartoint(char letter);
  16. char *encryption(char text[], char *key, char mode);
  17. char *generate_key();
  18. char correct_account_password[50];
  19. bool check_username(string username, FILE *account_file);
  20. bool check_password(string password, FILE *account_file);
  21. int main(void)
  22. {
  23. FILE *key = fopen("key.txt", "a");
  24. fclose(key);
  25. key = fopen("key.txt", "r");
  26. fgets(random_key, 6, key);
  27. if(strcmp(random_key, "") == 0)
  28. {
  29. strcpy(random_key, generate_key());
  30. }
  31. fclose(key);
  32. key = fopen("key.txt", "w");
  33. fputs(random_key, key);
  34. fclose(key);
  35. printf("Welcome to LocalPasswordManager! Do you have an account? Enter y for yes and n for no: ");
  36. string has_account = get_string();
  37. while(!(strcmp(has_account, "y") == 0 || strcmp(has_account, "n") == 0))
  38. {
  39. printf("Please enter either y for yes and n for no: ");
  40. has_account = get_string();
  41. }
  42. if(strcmp(has_account, "y") == 0)
  43. {
  44. FILE *accounts = fopen("accounts.txt", "r");
  45. printf("Please enter your username: ");
  46. string account_username = get_string();
  47. account_username = encryption(account_username, random_key, 'e');
  48. bool correct_username = false;
  49. while(!correct_username)//while the username is not correct
  50. {
  51. if (check_username(account_username, accounts))
  52. {
  53. correct_username = true;
  54. printf("Welcome\n");
  55. }
  56. else
  57. {
  58. printf("Please enter a valid username: ");
  59. account_username = get_string();
  60. account_username = encryption(account_username, random_key, 'e');
  61. fseek(accounts, 0, SEEK_SET);
  62. }
  63. }
  64. account_username = encryption(account_username, random_key, 'd');
  65. printf("%s, please enter your password: ", account_username);
  66. string account_password = get_string();
  67. account_password = encryption(account_password, random_key, 'e');
  68. char account_password_string[50];// <----------------------------// whY is this here
  69. strcpy(account_password_string, account_password);
  70. bool correct_password = false;
  71. while(!correct_password)
  72. {
  73. if(check_password(account_password_string, accounts))
  74. {
  75. correct_password = true;
  76. }
  77. else
  78. {
  79. printf("length of correct password: %lu\n", strlen(correct_account_password));
  80. printf("Please enter the correct password: ");
  81. printf("%s, %s, %s\n", correct_account_password, account_password_string, account_password);
  82. account_password = get_string();
  83. account_password = encryption(account_password, random_key, 'e');
  84. while(strcmp(account_password, correct_account_password) != 0)
  85. {
  86. printf("Please enter the correct password: ");
  87. account_password = get_string();
  88. account_password = encryption(account_password, random_key, 'e');
  89. }
  90. correct_password = true;
  91. }
  92. }
  93. account_password = encryption(account_password, random_key, 'd');
  94. printf("Welcome!\n");
  95. fclose(accounts);
  96. //open account of username.txt
  97. //enter accounts
  98. //enter name of account
  99. //enter username for account
  100. //enter password
  101. }
  102. else
  103. {
  104. FILE *accounts = fopen("accounts.txt", "a");
  105. printf("Please create a new username: ");
  106. // add this to create the suer file with user's name
  107. char account_username[50];
  108. FILE *fp;
  109. scanf("%s",account_username);
  110. fp=fopen(account_name,"w");
  111. fclose(fp);
  112. //string account_username = get_string();
  113. account_username = encryption(account_username, random_key, 'e');
  114. fputs(account_username, accounts);
  115. fputs("\t", accounts);
  116. account_username = encryption(account_username, random_key, 'd');
  117. printf("Please create a password: ");
  118. string account_password = get_string();
  119. char account_username_string[50];
  120. strcpy(account_username_string, account_username);
  121. account_password = encryption(account_password, random_key, 'e');
  122. fputs(account_password, accounts);
  123. fputs("\n", accounts);
  124. account_password = encryption(account_password, random_key, 'd');
  125. fclose(accounts);
  126. printf("Welcome, %s!\n", account_username_string);
  127. }
  128. //while(key != quit)
  129. //{
  130. //display options for resetting the password, accessing a specific website‘s username
  131. //and password, adding a website, deleting a website, or changing website information.
  132.  
  133. //if the user chooses to add a website, add the website information to the end of the file.
  134.  
  135.  
  136. //if the user chooses to delete website information, then replace the information with
  137. //spaces in the file and remove the website from the list.
  138.  
  139. //if the user chooses to access a website‘s information, then
  140. //find the information on the file and display it,
  141.  
  142. //if the user chooses to reset the one-time password, then decrypt the information
  143. //and encrypt it using the new one-time password.
  144.  
  145. //If the user chooses to change website information, then ask the user if they want to
  146. //change the name, the username, or the password, and then change the text in
  147. //the file appropriately
  148.  
  149. //here, ask the user what he or she would like to so
  150. //}
  151.  
  152. }
  153. int keychartoint(char letter)
  154. {
  155. int alpha_num = toupper(letter) - 65;
  156. return alpha_num;
  157. }
  158. char *encryption(char text[], char *key, char mode)
  159. {
  160. //MODES: e-encrypt, d-decrypt
  161. //char plain_text_array[50];
  162. //char *plain_text = " ";
  163. //char plain_text[50];
  164. strcpy(plain_text, text);
  165. if(mode == 'e'){
  166. }
  167. int key_char = 0;
  168. for (int text_char = 0; text_char < strlen(text); text_char++)
  169. {
  170. if (isalpha(text[text_char]))
  171. {
  172. int char_num = text[text_char];
  173. /*Different algorithms are used for upper and lower case
  174. characters because of their differences in the ASCII index.*/
  175. if (isupper(text[text_char]))
  176. {
  177. char_num -= 65;
  178. if(mode == 'e')
  179. {
  180. char_num = (char_num + keychartoint(key[key_char])) % 26;
  181. }
  182. else if(mode == 'd')
  183. {
  184. char_num = (char_num - keychartoint(key[key_char]) + 26) % 26;
  185. }
  186. char_num += 65;
  187. }
  188. else
  189. {
  190. char_num -= 97;
  191. if(mode == 'e')
  192. {
  193. char_num = (char_num + keychartoint(key[key_char])) % 26;
  194. }
  195. else if(mode == 'd')
  196. {
  197. char_num = (char_num - keychartoint(key[key_char]) + 26) % 26;
  198. }
  199. char_num += 97;
  200. }
  201. plain_text[text_char] = char_num;
  202. key_char++;
  203. /*This if statement returns back to the first character
  204. if all of the characters in the keyword have been used.*/
  205. if (key_char >= strlen(key))
  206. {
  207. key_char = 0;
  208. }
  209. }
  210. }
  211. return plain_text;
  212. }
  213.  
  214. char *generate_key()
  215. {
  216. static char string_key[6];
  217. srand(time(NULL));
  218. int rand_num = (rand() % 26) + 65;
  219. for(int i = 0; i < 5; i++)
  220. {
  221. string_key[i] = rand_num;
  222. rand_num = (rand() % 26) + 65;
  223. }
  224. return string_key;
  225. }
  226.  
  227. bool check_username(string username, FILE *account_file)
  228. {
  229. char buffer[50];
  230. char current_character = fgetc(account_file);
  231. int i = 0;
  232. while(current_character != EOF)//while the file has not ended
  233. {
  234. while(current_character != '\t')
  235. {
  236. buffer[i] = current_character;
  237. current_character = fgetc(account_file);
  238. i++;
  239. }
  240. if(strcmp(buffer, username) == 0)
  241. {
  242. return true;
  243. }
  244. else
  245. {
  246. while(current_character != '\n' && current_character != EOF)
  247. {
  248. current_character = fgetc(account_file);
  249. }
  250. }
  251. memset(buffer, 0, sizeof(buffer));
  252. i = 0;
  253. current_character = fgetc(account_file);
  254. }
  255. return false;
  256. }
  257.  
  258. bool check_password(string password, FILE *account_file)
  259. {
  260. char buffer[50];
  261. char current_character = fgetc(account_file);
  262. int i = 0;
  263. while(current_character != '\n')
  264. {
  265. if(current_character == '\n')
  266. {
  267. break;
  268. }
  269. buffer[i] = current_character;
  270. current_character = fgetc(account_file);
  271. i++;
  272. }
  273.  
  274. int j = 0;
  275. while(buffer[j] != '\0')
  276. {
  277. j++;
  278. }
  279. if(fgetc(account_file) != EOF)
  280. {
  281. buffer[j - 1] = 0;
  282. }
  283. if(strcmp(buffer, password) == 0)
  284. {
  285. return true;
  286. }
  287. else
  288. {
  289. strcpy(correct_account_password, buffer);
  290. return false;
  291. }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement