Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //cipher.c
- void encrypt();
- void decrypt(); //Done
- int checkExist(); //Done
- int checkEntry(char name1[]); //Done
- void print(); // done
- int newEntryMenu(); //done
- char c[1000];
- char * username[50];
- char * password[50];
- char * usertype[50];
- int shiftCode = 3;
- int lineCount;
- void decrypt(){
- if(checkExist() == 0)
- {
- printf("Does not exist");
- return;
- }
- FILE *pwfile;
- pwfile = fopen("password.csv","r");
- char * parts;
- lineCount = 0;
- while(!(fgets(c,sizeof(c),pwfile) == NULL)){
- //fgets(c,sizeof(c),pwfile);
- if(c == NULL) continue;
- // printf("The line is: %s and is line %d \n", c,lineCount);
- int i;
- for (i = 0; i<strlen(c); i++){
- if(c[i] == '\n' || c[i] == 10 || c[i] == 13 )
- {
- c[i] = 0;
- break;
- }
- //printf("Current is %c \n", c[i]);
- //printf("The character code before is %d\n", (int) c[i]);
- c[i] = c[i] - shiftCode;
- //printf("After is %c \n", c[i]);
- //printf("The character code after is %d\n", (int) c[i]);
- }
- // //printf("The line after decrypt: %s\n", c);
- // printf("Splitting string %s into tokens: \n",c);
- parts = strtok(c, ",");
- int tokenCount = 0;
- while(parts != NULL ){
- // printf("%s\n",parts);
- // printf("The tokencount is: %d\n",tokenCount);
- switch(tokenCount)
- {
- case 0:
- username[lineCount] = (char*) malloc(sizeof(char) * 100);
- strcpy(username[lineCount], parts);
- // printf("Username at line %d is %s\n",lineCount,username[lineCount] );
- break;
- case 1:
- password[lineCount] = (char*) malloc(sizeof(char) * 100);
- strcpy(password[lineCount], parts);
- // printf("Password at line %d is %s\n",lineCount,password[lineCount]);
- break;
- case 2:
- usertype[lineCount] = (char*) malloc(sizeof(char) * 100);
- strcpy(usertype[lineCount], parts);
- // printf("usertype at line %d is %s\n",lineCount,usertype[lineCount]);
- break;
- }
- tokenCount++;
- parts = strtok(NULL, ",");
- }
- // printf("\nCurrent array is: \n");
- // printf("User name[%d]: %s\n",lineCount,username[lineCount]);
- // printf("User pw[%d]: %s\n",lineCount,password[lineCount]);
- // printf("Usertype[%d]: %s\n",lineCount,usertype[lineCount]);
- lineCount++;
- }
- print();
- fclose(pwfile);
- return;
- }
- //checkExist()
- int checkEntry(char name1[]){
- int i;
- printf("I am here\n");
- printf("the name being checked is %s\n",name1);
- for(i=0;username[i] != NULL ;i++){
- if(strcmp(username[i],name1) == 0) return 1;
- }
- return 0;
- }
- void print(){
- printf("\n\nAnd the array is: \n");
- int j;
- for (j = 0; username[j] != NULL ;j++){
- printf("User name[%d]: %s\n",j,username[j]);
- printf("User pw[%d]: %s\n",j,password[j]);
- printf("Usertype[%d]: %s\n",j,usertype[j]);
- }
- return;
- }
- int checkExist(){
- FILE *pwfile;
- if(pwfile = fopen("password.csv","r")){
- fclose(pwfile);
- return 1;
- } else {
- printf("File does not exist and will be created now");
- pwfile = fopen("password.csv","w");
- fclose(pwfile);
- return 0;
- }
- }
- int newEntryMenu(){
- decrypt();
- char *newUsername = (char*) malloc(sizeof(char) * 100);
- char *newPassword = (char*) malloc(sizeof(char) * 100);
- char *newUsertype = (char*) malloc(sizeof(char) * 100);
- printf("Please enter the new username: ");
- fgets(newUsername,100,stdin);
- printf("user name is %s\n",newUsername);
- int i = strlen(newUsername)-1;
- //if( newUsername[i] == '\n')
- // newUsername[i] = '\0';
- //if(!checkEntry(newUsername)) return 0;
- printf("Please enter the new password: ");
- fgets(newPassword,100,stdin);
- i = strlen(newPassword)-1;
- if( newPassword[i] == '\n')
- newPassword[i] = '\0';
- printf("Please enter the new usertype: ");
- fgets(newUsertype,100,stdin);
- i = strlen(newUsertype)-1;
- if( newUsertype[i] == '\n')
- newUsertype[i] = '\0';
- printf("The username of %s with the password %s and of the type %s will be added\n",newUsername,newPassword,newUsertype);
- username[lineCount] = (char*) malloc(sizeof(char) * 100);
- password[lineCount] = (char*) malloc(sizeof(char) * 100);
- usertype[lineCount] = (char*) malloc(sizeof(char) * 100);
- strcpy(username[lineCount], newUsername);
- strcpy(password[lineCount], newPassword);
- strcpy(usertype[lineCount], newUsertype);
- print();
- encrypt();
- return;
- }
- void encrypt(){
- int i;
- char c[500];
- FILE *pwfile;
- checkExist();
- pwfile = fopen("password.csv","w");
- for(i=0;username[i] != NULL;i++){
- /*c = strcat(username[i],",");
- c = strcat(c,password[i]);
- c = strcat(password[i],",");
- c = strcat(c,usertype[i]);
- c = strcat(usertype[i],"\n");
- */
- sprintf(c,"%s,%s,%s\n",username[i],password[i],usertype[i]);
- printf("The unencrypt line is: %s",c);
- int j;
- for(j=0;j<strlen(c);j++){
- if(c[j] == '\n') break;
- c[j] = c[j] + shiftCode;
- }
- printf("The encrypted line is: %s",c);
- fprintf(pwfile,"%s",c);
- }
- fclose(pwfile);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement