2017-02-09 13 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"); 
    } 

非常に単純な暗号化/復号化プログラムです。 .csvファイルからスキーマを読み取り、入力キーとペアになって暗号化復号化情報を提供します。 Worksは完璧に私が必要とする全てを行い、期待される出力をプリントアウトします。しかし、私は後でセグメンテーションの失敗をダンプし、理由は分かりません。実行中にセグメンテーションコアダンプエラーが発生する

+0

私はインデントがコードを読みやすくすると考えられていたと思っています。 – Dmitri

+0

char * mappingFile;ここでは、mappingfileをcharecterポインタとして宣言し、配列として使用しています。if(strcmp(argv [1]、 "-i")== 0){mappingFile = argv [2];そのためにメモリを割り当てることなく、すなわち、あなたがmappingFileにスペースを割り当てず、あなたがそれにアクセスしています。したがって、ダンプ –

+0

申し訳ありませんが、ビットをクリーニングしようとしました – spaceinvaders101

答えて

0

私は間違いを見つけました...アウトオブブラケット。御時間ありがとうございます!

関連する問題