Advertisement
Guest User

Untitled

a guest
May 15th, 2017
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | None | 0 0
  1. /**
  2.  Corral, Jesus J
  3.  CSE7359 Lab 2 - BOF
  4.  02/26/2010
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #define DEBUG 0
  11. #define DEBUG_INFO {\
  12.     printf("\n");\
  13.     printf("\t+---- DEBUG INFO -----+\n");\
  14.     printf("\t| ACL : %s\n", aclFile);\
  15.     printf("\t| KEY : %s\n", keyFile);\
  16.     printf("\t| usr : %s\n", usr);\
  17.     printf("\t| psw : %s\n", psw);\
  18.     printf("\t| nStr: %s\n", newStr);\
  19.     printf("\t| key : %s\n", key);\
  20.     printf("\t| uPsw: %s\n", usrpsw);\
  21.     printf("\t+---------------------+\n");\
  22.     printf("\n");\
  23. }\
  24.  
  25. #define MAX_BUFFER      8
  26. #define LINE_BUFFER     32
  27. #define READ_LINE       255
  28. #define ENC_MAX_BUFFER (MAX_BUFFER*2)+1
  29. #define ACLFILENAME "acl.txt"
  30. #define KEYFILENAME "key.txt"
  31. #define ALPHA 26
  32.  
  33. int find(char str[], char c){
  34.     int i;
  35.     for (i = 0; i < strlen(str) && str[i]; i++)
  36.         if(str[i] == c) return i;
  37.     return -1;
  38. }
  39.  
  40. void concatenate(char newStr[], char str1[], char str2[]){
  41.     strcat(newStr, str1);
  42.     strcat(newStr, ":");
  43.     strcat(newStr, str2);
  44. }
  45.  
  46. void lowercase(char str[]){
  47.     int i;
  48.     for(i = 0; i < strlen(str) && str[i]; i++)
  49.         str[i] = tolower(str[i]);
  50. }
  51.  
  52. void uppercase(char str[]){
  53.     int i;
  54.     for(i = 0; i < strlen(str) && str[i]; i++)
  55.         str[i] = toupper(str[i]);
  56. }
  57.  
  58. void encode(char key[], char str[]){
  59.     lowercase(str);
  60.     int i;
  61.     for(i = 0; i < strlen(str) && str[i]; i++){
  62.         if('a' <= str[i] && str[i] <= 'z'){
  63.             str[i] = find(key, str[i]) + 'a';
  64.         }
  65.     }
  66.     uppercase(str);
  67. }
  68.  
  69. int validate(char str1[], char str2[]){
  70.     if(strlen(str1) != strlen(str2)) return 0;
  71.     int i;
  72.     for(i = 0; i < strlen(str1) && i < strlen(str2) ; i++){
  73.         if(str1[i] != str2[i]) return 0;
  74.     }
  75.     return 1;
  76. }
  77.  
  78. int main(char argc[], int argv[]){
  79.     char aclFile[strlen(ACLFILENAME) + 1];
  80.     char usr    [MAX_BUFFER + 1];
  81.     char psw    [MAX_BUFFER + 1];
  82.     char buf    [LINE_BUFFER];
  83.     char key    [ALPHA];
  84.     char line   [LINE_BUFFER];
  85.     char usrpsw [ENC_MAX_BUFFER];
  86.     char newStr [ENC_MAX_BUFFER];
  87.     char keyFile[strlen(KEYFILENAME) + 1];
  88.     int authenticated;
  89.    
  90.     while(1){
  91.         // Init all variables
  92.         strncpy(aclFile, ACLFILENAME, strlen(ACLFILENAME));
  93.         aclFile[strlen(ACLFILENAME)] = 0;
  94.         usr[0] = 0;
  95.         psw[0] = 0;
  96.         buf[0] = 0;
  97.         key[0] = 0;
  98.         usrpsw[0] = 0;
  99.         newStr[0] = 0;
  100.         strncpy(keyFile, KEYFILENAME, strlen(KEYFILENAME));
  101.         keyFile[strlen(KEYFILENAME)] = 0;
  102.         authenticated = 0;
  103.        
  104.         // Get user name       
  105.         printf("username: ");
  106.         gets(buf);
  107.         strncpy(usr, buf, MAX_BUFFER);
  108.         usr[MAX_BUFFER] = 0;
  109.        
  110.         // Get password
  111.         printf("password: ");
  112.         gets(buf);
  113.         strncpy(psw, buf, MAX_BUFFER);
  114.         psw[MAX_BUFFER] = 0;
  115.  
  116.         // create new string
  117.         concatenate(newStr, usr, psw);
  118.        
  119.     #if DEBUG
  120.         DEBUG_INFO;
  121.     #endif
  122.         // open key file and get key.
  123.         FILE * pFile = fopen(keyFile, "r");
  124.         if(pFile != NULL){
  125.             fgets(key, ALPHA, pFile);
  126.             fclose(pFile);
  127.            
  128.             // open acl file and get encoded usr:psw.
  129.             pFile = fopen(aclFile, "r");
  130.             if(pFile != NULL){
  131.                 fgets(line, READ_LINE, pFile);
  132.                 strncpy(usrpsw, line, ENC_MAX_BUFFER);
  133.                 usrpsw[ENC_MAX_BUFFER] = 0;
  134.                 fclose(pFile);
  135.            
  136.                 // encode string
  137.                 encode(key, newStr);
  138.    
  139.     #if DEBUG
  140.         DEBUG_INFO;
  141.     #endif
  142.                 // authenticate user
  143.                 authenticated = validate(usrpsw, newStr);
  144.             }
  145.         } // end of if open file key.
  146.        
  147.         if(authenticated){
  148.             printf("Congratulations!!! You hacked my program!!!\n");
  149.             printf("Please enter your name: ");
  150.             pFile = fopen("log.txt", "a");
  151.             if(pFile != NULL){
  152.                 gets(buf);
  153.                 fprintf(pFile, "%s\n", buf);
  154.                 fclose(pFile);
  155.             }
  156.             return 0;
  157.         } else {
  158.             printf("Login failed...\n");
  159.         }
  160.         printf("\n");
  161.     }
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement