Advertisement
Guest User

Untitled

a guest
May 13th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1. //cipher.c
  2.  
  3.  
  4.  
  5.  
  6.  
  7. void encrypt();
  8. void decrypt(); //Done
  9. int checkExist(); //Done
  10. int checkEntry(char name1[]); //Done
  11. void print(); // done
  12. int newEntryMenu(); //done
  13.  
  14. char c[1000];
  15. char * username[50];
  16. char * password[50];
  17. char * usertype[50];
  18. int shiftCode = 3;
  19. int lineCount;
  20.  
  21. void decrypt(){
  22.     if(checkExist() == 0)
  23.     {
  24.     printf("Does not exist");
  25.     return;
  26.     }
  27.     FILE *pwfile;
  28.    
  29.     pwfile = fopen("password.csv","r");
  30.     char * parts;
  31.     lineCount = 0;
  32.     while(!(fgets(c,sizeof(c),pwfile) == NULL)){
  33.         //fgets(c,sizeof(c),pwfile);
  34.         if(c == NULL) continue;
  35.     //  printf("The line is: %s and is line %d \n", c,lineCount);
  36.         int i;
  37.         for (i = 0; i<strlen(c); i++){
  38.             if(c[i] == '\n' || c[i] == 10 || c[i] == 13 )
  39.                 {  
  40.                     c[i] = 0;
  41.                     break;
  42.                 }
  43.             //printf("Current is %c \n", c[i]);
  44.             //printf("The character code before is %d\n", (int) c[i]);
  45.             c[i] = c[i] - shiftCode;
  46.             //printf("After is %c \n", c[i]);
  47.             //printf("The character code after is %d\n", (int) c[i]);
  48.         }
  49. //      //printf("The line after decrypt: %s\n", c);
  50. //      printf("Splitting string %s into tokens: \n",c);
  51.         parts = strtok(c, ",");
  52.         int tokenCount = 0;
  53.         while(parts != NULL ){
  54.     //      printf("%s\n",parts);
  55.     //      printf("The tokencount is: %d\n",tokenCount);
  56.             switch(tokenCount)
  57.             {
  58.                 case 0:
  59.                     username[lineCount] = (char*) malloc(sizeof(char) * 100);
  60.                     strcpy(username[lineCount], parts);
  61.     //              printf("Username at line %d is %s\n",lineCount,username[lineCount] );
  62.                     break;
  63.                 case 1:
  64.                     password[lineCount] = (char*) malloc(sizeof(char) * 100);
  65.                     strcpy(password[lineCount], parts);
  66.     //              printf("Password at line %d is %s\n",lineCount,password[lineCount]);
  67.                     break;
  68.                 case 2:
  69.                     usertype[lineCount] = (char*) malloc(sizeof(char) * 100);
  70.                     strcpy(usertype[lineCount], parts);
  71.     //          printf("usertype at line %d is %s\n",lineCount,usertype[lineCount]);                   
  72.                     break;
  73.             }
  74.            
  75.             tokenCount++;
  76.            
  77.             parts = strtok(NULL, ",");
  78.         }
  79.     //  printf("\nCurrent array is: \n");
  80.     //  printf("User name[%d]: %s\n",lineCount,username[lineCount]);
  81.     //      printf("User pw[%d]: %s\n",lineCount,password[lineCount]);
  82.     //      printf("Usertype[%d]: %s\n",lineCount,usertype[lineCount]);
  83.         lineCount++;
  84.     }
  85.     print();
  86.        
  87.     fclose(pwfile);
  88.     return;
  89.    
  90. }
  91.  
  92. //checkExist()
  93. int checkEntry(char name1[]){
  94.     int i;
  95.     printf("I am here\n");
  96.     printf("the name being checked is %s\n",name1);
  97.    
  98.     for(i=0;username[i] != NULL ;i++){
  99.         if(strcmp(username[i],name1) == 0) return 1;
  100.     }
  101.     return 0;
  102. }
  103. void print(){
  104.     printf("\n\nAnd the array is: \n");
  105.     int j;
  106.         for (j = 0; username[j] != NULL ;j++){
  107.             printf("User name[%d]: %s\n",j,username[j]);
  108.             printf("User pw[%d]: %s\n",j,password[j]);
  109.             printf("Usertype[%d]: %s\n",j,usertype[j]);
  110.         }
  111.     return;
  112. }
  113. int checkExist(){
  114.     FILE *pwfile;
  115.     if(pwfile = fopen("password.csv","r")){
  116.         fclose(pwfile);
  117.         return 1;
  118.     } else {
  119.         printf("File does not exist and will be created now");
  120.         pwfile = fopen("password.csv","w");
  121.         fclose(pwfile);
  122.         return 0;
  123.     }
  124. }
  125. int newEntryMenu(){
  126.     decrypt();
  127.     char *newUsername = (char*) malloc(sizeof(char) * 100);
  128.     char *newPassword = (char*) malloc(sizeof(char) * 100);
  129.     char *newUsertype = (char*) malloc(sizeof(char) * 100);
  130.    
  131.     printf("Please enter the new username: ");
  132.     fgets(newUsername,100,stdin);
  133.     printf("user name is %s\n",newUsername);
  134.     int i = strlen(newUsername)-1;
  135.         //if( newUsername[i] == '\n')
  136.         //      newUsername[i] = '\0';
  137.         //if(!checkEntry(newUsername)) return 0;
  138.     printf("Please enter the new password: ");
  139.     fgets(newPassword,100,stdin);
  140.     i = strlen(newPassword)-1;
  141.         if( newPassword[i] == '\n')
  142.                 newPassword[i] = '\0';
  143.     printf("Please enter the new usertype: ");
  144.     fgets(newUsertype,100,stdin);
  145.     i = strlen(newUsertype)-1;
  146.         if( newUsertype[i] == '\n')
  147.                 newUsertype[i] = '\0';
  148.     printf("The username of %s with the password %s and of the type %s will be added\n",newUsername,newPassword,newUsertype);
  149.    
  150.     username[lineCount] = (char*) malloc(sizeof(char) * 100);
  151.     password[lineCount] = (char*) malloc(sizeof(char) * 100);
  152.     usertype[lineCount] = (char*) malloc(sizeof(char) * 100);
  153.     strcpy(username[lineCount], newUsername);
  154.     strcpy(password[lineCount], newPassword);
  155.     strcpy(usertype[lineCount], newUsertype);
  156.     print();
  157.     encrypt();
  158.     return;
  159.    
  160. }
  161. void encrypt(){
  162. int i;
  163. char c[500];
  164. FILE *pwfile;
  165.     checkExist();
  166.     pwfile = fopen("password.csv","w");
  167. for(i=0;username[i] != NULL;i++){
  168.     /*c = strcat(username[i],",");
  169.     c = strcat(c,password[i]);
  170.     c = strcat(password[i],",");
  171.     c = strcat(c,usertype[i]);
  172.     c = strcat(usertype[i],"\n");
  173.     */
  174.     sprintf(c,"%s,%s,%s\n",username[i],password[i],usertype[i]);
  175.     printf("The unencrypt line is: %s",c);
  176.     int j;
  177.     for(j=0;j<strlen(c);j++){
  178.         if(c[j] == '\n') break;
  179.         c[j] = c[j] + shiftCode;
  180.     }
  181.     printf("The encrypted line is: %s",c);
  182.     fprintf(pwfile,"%s",c);
  183.    
  184. }
  185. fclose(pwfile);
  186. return;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement