2017-02-09 3 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 파일에서 스키마를 읽습니다. 스키마는 입력 키와 쌍을 이루면 암호화 해독 정보를 제공합니다. 작품은 내가 필요로하는 모든 것을 완벽하게 수행하고 예상되는 결과물을 인쇄합니다. 하지만 나중에 세그먼트 분할 오류가 발생하고 이유를 모르겠다.실행 중 세그먼트 덤프 오류 발생

+0

필자는 들여 쓰기가 코드를 더 쉽게 만들 수 있다고 생각했는데, 읽기 쉽도록 일관성을 유지했다. – Dmitri

+0

char * mappingFile; 여기에 매핑 파일을 charecter 포인터로 선언하고이를 배열로 사용했습니다. if (strcmp (argv [1], "-i") == 0) {mappingFile = argv [2]; 그것을 위해 메모리를 할당하지 않고, 즉; mappingFile에 사용자가 할당 한 공간이없고 액세스하고 있습니다. 그러므로 덤프 –

+0

미안, 미안, 비트를 청소하려고 했어 – spaceinvaders101

답변

관련 문제