Advertisement
Guest User

Untitled

a guest
Oct 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. typedef struct Users {
  8. char Id[12];
  9. char hashPW[5];
  10. } User;
  11.  
  12. User *user;
  13. void filePopulate();
  14. unsigned int rand32Gen();
  15. char * createXOR();
  16. void writeFile();
  17. void R();
  18. void E(char *in, char *out);
  19. char * Hashify(int n, char * password);
  20. void E(char *in, char *out)
  21. {
  22. //make the input characters uppercase.
  23. char c0 = toupper(in[0]);
  24. char c1 = toupper(in[1]);
  25. char c2 = toupper(in[2]);
  26. char c3 = toupper(in[3]);
  27.  
  28. out[0] = (c0 & 0x80) ^ (((c0 >> 1) & 0x7F) ^ ((c0) & 0x7F));
  29. out[1] = ((c1 & 0x80) ^ ((c0 << 7) & 0x80)) ^ (((c1 >> 1) & 0x7F) ^ ((c1) & 0x7F));
  30. out[2] = ((c2 & 0x80) ^ ((c1 << 7) & 0x80)) ^ (((c2 >> 1) & 0x7F) ^ ((c2) & 0x7F));
  31. out[3] = ((c3 & 0x80) ^ ((c2 << 7) & 0x80)) ^ (((c3 >> 1) & 0x7F) ^ ((c3) & 0x7F));
  32. }
  33. char * Hashify(int n, char * password){
  34. //allocate a string that is initially cleared
  35. char * hash = (char*)calloc(12, sizeof(char));
  36. //if the allocation fails print error message
  37. if (hash == NULL)
  38. {
  39. printf("calloc returned NULL in hashify\n");
  40. return hash;
  41. }
  42. int i;
  43. //iterate through every 4 byte chunk and perform the hashing function
  44. for (i = 0; i < n; i++)
  45. {
  46. E(&password[4*i], &hash[4*i]);
  47. }
  48. return hash;
  49. }
  50.  
  51. int main(int argc, const char * argv[])
  52. {
  53. unsigned int uniqueNum;
  54. char enteredID[5];
  55. char enteredPass[5];
  56. user = (User*)calloc(100, sizeof(int));
  57. int Id=-1;
  58. int attempts=5;
  59.  
  60. strcpy(user[0].Id,"user1");
  61. strcpy(user[1].Id,"user2");
  62. strcpy(user[2].Id,"user3");
  63. strcpy(user[3].Id,"user4");
  64.  
  65. strcpy(user[0].hashPW,Hashify(1,"pass1"));
  66. strcpy(user[1].hashPW,Hashify(1,"pass2"));
  67. strcpy(user[2].hashPW,Hashify(1,"pass3"));
  68. strcpy(user[3].hashPW,Hashify(1,"pass4"));
  69.  
  70.  
  71. writeFile();
  72. uniqueNum = rand32Gen();
  73.  
  74. while(1){
  75.  
  76. printf("Please enter an already existing User ID: ");
  77. fgets(enteredID, 32, stdin);
  78. int i;
  79. for(i=0;i<4;i++)
  80. {
  81. if (Id>0) break;
  82. else
  83. if(strncmp(enteredID,user[i].Id,5)==0)
  84. {
  85. Id=i;
  86. printf("User ID is in the table! \n");
  87.  
  88.  
  89. while(1)
  90. {
  91. if(attempts==0)
  92. {
  93. printf("MAX attempts reached EXITING\n");
  94. return 0;
  95. }
  96. printf("Please enter a password for %s : ",user[i].Id);
  97. fgets(enteredPass, 32, stdin);
  98. //ask for a password for the username that was typed above then
  99.  
  100.  
  101.  
  102. char * hashpass=Hashify(1,enteredPass);
  103. char * userxor = createXOR(hashpass,uniqueNum);
  104. char * tablexor = createXOR(user[Id].hashPW,uniqueNum);
  105.  
  106.  
  107.  
  108. // then user strcmp to check userxor is equal to tablexor if they are then access granted
  109. int token = strncmp(userxor,tablexor,5);
  110.  
  111. if(token == 0)
  112. {
  113. printf("Access GRANTED \n");
  114. return 0;
  115. }
  116. else
  117. {
  118. printf("Access DENIED!\n");
  119. attempts--;
  120. }
  121.  
  122. }
  123. }
  124. }
  125.  
  126. printf("User not in table , enter an existing user!\n");
  127. continue;
  128. }
  129.  
  130.  
  131.  
  132. }
  133.  
  134. void filePopulate()
  135. {
  136. // Create and open the file, we need rw access since we may have to write to it
  137. FILE *userFile;
  138. FILE *hashFile;
  139.  
  140. userFile = fopen("users.txt","r");
  141. hashFile = fopen("hash.txt","r");
  142. int i = 0;
  143.  
  144. // Go through the file and populate the array for userIDs
  145. while(fscanf(userFile, "%s", user[i].Id) != EOF)
  146. ++i;
  147.  
  148. i = 0;
  149.  
  150. // Obviously in this case the hashPWs are ordered in the same order the userIDs are ordered
  151. while(fscanf(hashFile, "%s", user[i].hashPW) != EOF)
  152. ++i;
  153.  
  154. fclose(userFile);
  155. fclose(hashFile);
  156. }
  157.  
  158. void writeFile()
  159. {
  160. FILE *userFile;
  161. FILE *hashFile;
  162.  
  163. // printf("I ran\n");
  164.  
  165. // Open both files for writing
  166. if ((userFile = fopen("user.txt", "w+")) == NULL)
  167. printf("Couldn't create file (userFile.txt)\n");
  168. if ((hashFile = fopen("hash.txt", "w+")) == NULL)
  169. printf("Couldn't create file (hashFile.txt)\n");
  170.  
  171. // Write to user file and hash file
  172. for (int i = 0; i < 5; i++)
  173. fprintf(userFile, "%s\n", user[i].Id);
  174. for (int i = 0; i < 5; i++)
  175. fprintf(hashFile, "%s\n", user[i].hashPW);
  176.  
  177. //printf("File written.\n");
  178.  
  179. fclose(userFile);
  180. fclose(hashFile);
  181. }
  182.  
  183. unsigned int rand32Gen()
  184. {
  185. unsigned int x;
  186. srand(time(NULL)); // seed to create a true random number
  187. x = rand() & 0xff;
  188. x |= (rand() & 0xff) << 8;
  189. x |= (rand() & 0xff) << 16;
  190. x |= (rand() & 0xff) << 24;
  191.  
  192. return (x);
  193. }
  194.  
  195.  
  196. char * createXOR(char* hash, unsigned int r)
  197. {
  198. int MAX_PASSWORD_LENGTH = 12;
  199. char * out = (char*)calloc(MAX_PASSWORD_LENGTH, sizeof(char));
  200. R(hash, out, r);
  201. R(&hash[4], &out[4], r);
  202. R(&hash[8], &out[8], r);
  203. return out;
  204. }
  205.  
  206. //xor function for each bundle (4 bytes)
  207. void R(char* in, char* out, unsigned int r)// r is the unique number
  208. {
  209. out[3] = in[3] ^ (r & 255); //xor the first byte with r's first byte
  210. r = r >> 8; //shift r by one byte
  211. out[2] = in[2] ^ (r & 255);
  212. r = r >> 8;
  213. out[1] = in[1] ^ (r & 255);
  214. r = r >> 8;
  215. out[0] = in[0] ^ (r & 255);
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement