Advertisement
Guest User

Untitled

a guest
Jan 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 KB | None | 0 0
  1. #include "StudentFunction.h"
  2.  
  3. void CreateUser(struct User* userData, int numberOfUsers)
  4. {
  5.  
  6.     printf("Choose your username ( max 64 characters and no space ): ");
  7.     scanf("%s", userData->username);
  8.  
  9.     printf("Choose your password ( max 64 characters and no space ): ");
  10.     scanf("%s", userData->password);
  11.  
  12.     printf("Your username is: %s \nYour password is: %s ", userData->username, userData->password);
  13.    
  14.     srand((unsigned)time(NULL));
  15.     srand((unsigned)time(NULL));
  16.    
  17.     for (int j = 0; j < numberOfUsers; j++)
  18.     {
  19.         int keyLo = rand() % 6 + 3;
  20.  
  21.         for (int i = 0; i < keyLo; i++)
  22.         {
  23.             userData[j].key[i] = rand() % 94 + 33;
  24.         }
  25.         userData[j].key[keyLo] = '\0';
  26.     }
  27.  
  28.    
  29.  
  30. }
  31.  
  32. void encryped(char text[], char encryptdText[], char key[])
  33. {
  34.     int i = 0;
  35.     int j = 0;
  36.     int lengthOfText = strlen(text);
  37.  
  38.         while (i < lengthOfText)
  39.         {
  40.             if (j == strlen(key))
  41.             {
  42.                 j = 0;
  43.             }
  44.             if (text[i] <= 32 || text[i] >= 126 || text[i] =='n'|| text[i] == '\0')
  45.             {
  46.                 encryptdText[i] = text[i];
  47.             }
  48.             else
  49.             {
  50.                 encryptdText[i] = (text[i] - 32 + key[j] - 32) % 95 + 32;
  51.                 j++;
  52.             }
  53.             i++;
  54.         }
  55. }
  56.  
  57. int LogIn(struct User* userData, int numberOfUsers)
  58. {
  59.     char username[64];
  60.     char password[64];
  61.     int a = 1;
  62.     int check = 0;
  63.     int position;
  64.  
  65.     for (int i = 0; i < numberOfUsers; i++)
  66.     {
  67.         printf("%s\t%s\n", userData[i].username, userData[i].password);
  68.     }
  69.  
  70.     while (a == 1)
  71.     {
  72.        
  73.         printf("Enter your Username: \n");
  74.         scanf("%s", &username);
  75.         getchar();
  76.         printf("Enter your Password: \n");
  77.         scanf("%s", &password);
  78.         getchar();
  79.         for (int i = 0; i < numberOfUsers; i++)
  80.         {
  81.             if ((strcmp(username, userData[i].username) != 0) || strcmp(password, userData[i].password) != 0)
  82.             {
  83.             }
  84.         else
  85.         {
  86.             check = 1; // checkar om dom är lika varanda
  87.             position = i; //vilken postion användaren är på
  88.             i = numberOfUsers;
  89.             a = 0;
  90.             printf("Login correct!");
  91.         }
  92.            
  93.         }
  94.  
  95.     }
  96.     return position;
  97. }
  98.  
  99. void StartMenu()
  100. {
  101.     printf("\n     MENU      \n");
  102.     printf("----------------\n");
  103.  
  104.     printf("1. Log In. \n");
  105.     printf("2. Create user.\n");
  106.     printf("3. Quit program. \n");
  107. }
  108. void MainMenu(struct User* userData, int userID, int numberOfUsers)
  109. {
  110.  
  111.     int choice = 1;
  112.     char textIn[1000];
  113.     char newfilename[1000];
  114.     FILE* pFileText;
  115.     pFileText = fopen("text.txt", "a+");
  116.     struct User pUser;
  117.     char fileSaveChoice;
  118.     char fileConext[1000]; //BYTTTTTNAMNNNN!!!
  119.     char newPass[64];
  120.     char encryptdText[1000];
  121.  
  122.     while (choice != -1)
  123.     {
  124.     /*char fileChoice;*/
  125.     char fileChoice[1000];
  126.  
  127.     printf("\n\n_______|MENU|__________\n");
  128.     printf("\n");
  129.  
  130.     printf("1. Input your text \n");
  131.     printf("2. Save your text to a new file \n");
  132.     printf("3. Save your text in an already existing file \n");
  133.     printf("4. Choose a file to read and present it's content (uncrypted) \n");
  134.     printf("5. Change your password\n");
  135.     printf("6. log out \n");
  136.     printf("7. exit program \n");
  137.        
  138.        
  139.         scanf("%d", &choice);
  140.         getchar();
  141.  
  142.         switch (choice)
  143.         {
  144.  
  145.  
  146.         case 1:
  147.            
  148.             printf("Write your text: ");
  149.             fgets(textIn, 1000, stdin);
  150.             printf("%s", textIn);
  151.  
  152.            
  153.             printf("do you want to save your text in the existing document? (y/n)?:  ");
  154.             scanf("%c", &fileSaveChoice);
  155.            
  156.            
  157.             if (fileSaveChoice == 'y')
  158.             {
  159.             fputs(textIn, pFileText);
  160.             fclose(pFileText);
  161.            
  162.             }
  163.             else
  164.             {
  165.                 printf("\nPlease chose a new option\n");
  166.                 choice = 0;
  167.             }
  168.             break;
  169.  
  170.         case 2:
  171.             printf("\n%s\n", textIn); //remove!!!//
  172.  
  173.             printf("chose a new name for the file");
  174.             scanf("%s", newfilename);
  175.            
  176.             FILE *pNewFile;
  177.             pNewFile = fopen(newfilename, "w");
  178.             encryped(textIn, encryptdText, userData->key);
  179.  
  180.             fputs(textIn, pNewFile);
  181.             fclose(pNewFile);
  182.  
  183.             break;
  184.        
  185.         case 3:
  186.             printf("save your text to an already existing file \n");
  187.             printf("chose an existing file: ");
  188.             scanf("%s", pUser.fileName);
  189.             getchar();
  190.  
  191.             FILE * pExistingfile;
  192.             pExistingfile = fopen(pUser.fileName, "a+");
  193.  
  194.             fputs(textIn, pExistingfile);
  195.             fclose(pExistingfile);
  196.             break;
  197.  
  198.         case 4:
  199.             printf("what file would you like to choose?: ");
  200.             scanf("%s", fileChoice);
  201.            
  202.             FILE * fileChoicePointer;
  203.             fileChoicePointer = fopen(fileChoice, "r");
  204.            
  205.             while (fgets(fileConext, sizeof(fileConext), fileChoicePointer) != NULL)
  206.             {
  207.                 printf("\n");
  208.                 fputs(fileConext, stdout);
  209.             }
  210.             fclose(fileChoicePointer);
  211.             break;
  212.            
  213.         case 5:
  214.             printf("Write your new password: ");
  215.             scanf("%s", newPass);
  216.  
  217.             strcpy(userData[userID].password, newPass);
  218.            
  219.             printf("%s", userData[userID].password);
  220.  
  221.             FILE* outFile;
  222.             outFile = fopen("User.txt", "r+");
  223.             fseek(outFile, 3, SEEK_SET);
  224.             for (int i = 0; i < numberOfUsers; i++)
  225.             {
  226.                 fprintf(outFile, "%s\t%s\n", userData[i].username, userData[i].password);
  227.             }
  228.             fclose(outFile);
  229.  
  230.             break;
  231.  
  232.         case 6:
  233.             printf("\nlogging out\n");
  234.             StartMenu();
  235.             choice = -1;
  236.             break;
  237.         case 7:
  238.             printf("exit");
  239.             exit(1);
  240.             break;
  241.         default:
  242.             break;
  243.         }
  244.    
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement