2017-02-09 6 views
0
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 


void encrypt(char inputText[20], int inputLength, int key); 
void decrypt(int cipherText[20], int inputLength, int key); 


FILE* fp; 
char* mappingFile; 

int main(int argc, char* argv[]){ 

    char inputText[20],temp; 
    int key,mode,inputLength, cipherText[20],i; 
    if (strcmp(argv[1], "-i")==0){ 
     mappingFile=argv[2]; 
    }else if (strcmp(argv[1],"-k")==0){ 
     key=atoi(argv[2]); 
    }else if (strcmp(argv[1],"-m")==0){ 
     mode = atoi(argv[2]); 
    }else{ 
     printf("invalid argument %s. Please re-run the program\n",argv[1]); 
       exit(EXIT_FAILURE); 
    } 
    if (strcmp(argv[3],"-i")==0){ 
    mappingFile = argv[4]; 
    }else if (strcmp(argv[3],"-k")==0){ 
    key=atoi(argv[4]); 
    }else if (strcmp(argv[3],"-m")==0){ 
    mode = atoi(argv[4]); 
    }else{printf("invalid argument %s. Please re-run the program\n",argv[3]); 
     exit(EXIT_FAILURE); 
    } 
    if (strcmp(argv[5],"-i")==0){ 
     mappingFile=argv[6]; 
    }else if (strcmp(argv[5],"-k")==0){ 
     key=atoi(argv[6]); 
    }else if (strcmp(argv[5],"-m")==0){ 
     mode=atoi(argv[6]); 
    }else{ 
     printf("invalid argument %s. Please re-run the program\n",argv[5]); 
     exit(EXIT_FAILURE); 
    } 
    if (key >25){ 
     printf("You entered %d. Sorry, your key must be between 1 and 25. Re-     run the program and try again\n", key); 
     exit(EXIT_FAILURE); 
    } 
    if (mode !=1 &&mode!=2){ 
     printf("Unidentified mode. Run again!\n"); 
     exit(EXIT_FAILURE); 
    } 
    fp=fopen(mappingFile,"r"); 
    if (fp==NULL){ 
     printf("Cannot open mapping file\n"); 
     exit(EXIT_FAILURE); 
    } 
    if (mode==1){ 
    printf("Enter the word you want to encrypt, upto 20 characters "); 
    scanf("%20s", inputText); 
    inputLength= strlen(inputText); 

    encrypt(inputText, inputLength, key); 
    } 
    if (mode==2){ 
    printf("Enter the encrypted word you want to decrypt, upto 20 soace-  separated numbers. Put any letter at the end of your message "); 
    i = 0; 
    printf("Enter the encrypted word you want to decrypt, upto 20 soace-separated numbers. Put any letter at the end of your message "); 
    i = 0; 
    do{ 
     scanf("%d%c",&cipherText[i],&temp); 
     i++; 
     } while (temp ==' '); 
     } 
     inputLength=i; 
     decrypt(cipherText, inputLength,key); 
     } 

void encrypt(char inputText[20], int inputLength, int key){ 
    int i,a,numb,character; 
    char inputLetter,letter,String[20]; 
    for(a=0;a<=(inputLength-1);a = a+1){ 
     inputLetter = inputText[a]; 
     fopen(mappingFile,"r"); 
     while (fscanf(fp, "%c, %d", &letter, &numb)!=EOF){ 
      if (letter == inputLetter){ 
       String[a]=(numb-key+26); 
       fclose(fp); 
       break; 
      } 
     } 
    } 
    for (i=0;i<(inputLength);i++){ 
     character = String[i]; 
     printf("%d ",character); 
    } 
    printf("\n"); 
} 

void decrypt(int cipherText[20], int inputLength, int key){ 
    int i,a,numb,temp,inputNumber,character; 
    char String[20],letter; 

    for(a=0;a<=(inputLength-1);a = a+1){ 
     inputNumber = cipherText[a]; 
     i = 0; 
     fopen(mappingFile,"r"); 
     temp=(inputNumber+key); 
     if (temp>26){ 
      temp = temp -26; 
     } 
     while (fscanf(fp, "%c, %d", &letter, &numb)!=EOF){ 
      if (numb == temp){ 
       String[a] = letter; 
       fclose(fp); 
       break; 
      }else{ 
       i++; 
      } 
     } 
     } 

     for (i=0;i<(inputLength);i++){ 
      character = String[i]; 
      printf("%c",character); 
     } 
     printf("\n"); 
    } 

Ein sehr, sehr einfaches Verschlüsselungs-/Entschlüsselungsprogramm. Aus einer CSV-Datei liest es ein Schema ein, das, wenn es mit dem Eingabeschlüssel gekoppelt ist, die Verschlüsselungsentschlüsselungsinformationen bereitstellt. Funktioniert perfekt mit allem, was ich brauche, druckt die erwartete Ausgabe aus. Aber ich bekomme Segmentierung Fehler nachher und ich habe keine Ahnung, warumErhalten von Segmentierungs-Core-Dump-Fehler beim Ausführen

+0

Ich dachte, Einrückung sollte Code * einfacher machen * zu lesen ... etwas Konsistenz würde helfen. – Dmitri

+0

char * mappingFile; Hier haben Sie mappingfile als Zeichenzeiger deklariert und als array if verwendet (strcmp (argv [1], "-i") == 0) {mappingFile = argv [2]; ohne dafür Speicher zuzuweisen, dh; Ihnen wird von mappingFile kein Speicherplatz zugewiesen und Sie greifen darauf zu. Daher dump –

+0

Sorry, versuchte es ein wenig zu reinigen – spaceinvaders101

Antwort

0

Ich fand den Fehler ... eine veraltete Klammer. Vielen Dank für Ihre Zeit!

Verwandte Themen