2013-08-20 3 views
0

나는 vigenere 암호를 사용하여 메시지를 암호화하고 해독하는 프로그램을 만들었습니다.여분의 문자가 파일에 인쇄되어 있습니다.

텍스트를 암호화하거나 해독하는 동안 일부 여분의 가비지 값이 인쇄됩니다.

그것은 당신이 input.txt 파일에 메시지를 작성해야하고 실행하는 동안 당신은 키 (영숫자 문자와 단어를) 제공해야 output.txtinput.txt 출력라는 이름의 파일에서 입력을합니다.

왜 이런 일이 발생하고 있습니까? 다음과 같이

코드는 다음과 같습니다

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 
#include<ctype.h> 



int Encrypt(char key[])   // CODE FOR ENCRYPTION 
{ 
    int sz = 0, i; 
    FILE *ifp, *ofp; 
    ifp = fopen("input.txt", "r"); 
    char *buffer; 
    char outputFilename[] = "output.txt"; 


    if (ifp == NULL) 
    { 
     fprintf(stderr, "Cant open input file\n"); 
     exit(1); 
    } 


    fseek(ifp, 0, SEEK_END); 
    sz = ftell(ifp); 

    // printf("%d",sz); 

    fseek(ifp, 0, SEEK_SET); 


    /* allocate memory for entire content */ 
    buffer = (char *)malloc(sizeof(char) * sz); 
    if (!buffer) 
     fclose(ifp), fputs("memory alloc fails", stderr), exit(1); 

    /* copy the file into the buffer */ 
    if (1 != fread(buffer, sz, 1, ifp)) 
     fclose(ifp), free(buffer), fputs("entire read fails", stderr), exit(1); 


    ofp = fopen(outputFilename, "w"); 

    if (ofp == NULL) 
    { 
     fprintf(stderr, "Can't open output file !\n"); 
    } 
    // fprintf(ofp,"%s",buffer); 

    int j = 0; 
    for (i = 0; i < strlen(buffer); i++) 
    { 
     if (j > strlen(key) - 1) 
      j = 0; 
     if (buffer[i] >= 65 && buffer[i] < 91) 
     { 

      int c = ((((buffer[i] - 65) + ((key[j] - 65) % 26))) % 26) + 65; 
      fprintf(ofp, "%c", c); 

     } 
     else if (buffer[i] >= 97 && buffer[i] < 123) 
     { 
      int c = ((((buffer[i] - 97) + ((key[j] - 65) % 26))) % 26) + 97; 

      fprintf(ofp, "%c", toupper(c)); 

     } 
     else 
     { 
      fprintf(ofp, "%c", buffer[i]); 
      continue; 
     } 
     j++; 
    } 
    printf("\n"); 

    fclose(ifp); 
    fclose(ofp); 

    return 0; 
} 

int Decrypt(char key[])   // CODE FOR DECRYPTION 
{ 

    int sz = 0, i; 
    FILE *ifp, *ofp; 
    ifp = fopen("output.txt", "r"); 
    char *buffer; 
    char outputFilename[] = "output2.txt"; 


    if (ifp == NULL) 
    { 
     fprintf(stderr, "Cant open input file\n"); 
     exit(1); 
    } 


    fseek(ifp, 0, SEEK_END); 
    sz = ftell(ifp); 

    // printf("%d",sz); 

    fseek(ifp, 0, SEEK_SET); 

    /* allocate memory for entire content */ 
    buffer = (char *)malloc(sizeof(char) * sz); 
    if (!buffer) 
     fclose(ifp), fputs("memory alloc fails", stderr), exit(1); 

    /* copy the file into the buffer */ 
    if (1 != fread(buffer, sz, 1, ifp)) 
     fclose(ifp), free(buffer), fputs("entire read fails", stderr), exit(1); 


    ofp = fopen(outputFilename, "w"); 

    if (ofp == NULL) 
    { 
     fprintf(stderr, "Can't open output file !\n"); 
    } 
    // fprintf(ofp,"%s",buffer); 

    int j = 0; 
    for (i = 0; i < strlen(buffer); i++) 
    { 
     if (j > strlen(key) - 1) 
      j = 0; 
     if (buffer[i] >= 65 && buffer[i] < 91) 
     { 
      if (buffer[i] > key[j]) 
      { 
       int c = 
        ((((buffer[i] - 65) - ((key[j] - 65) % 26))) % 26) + 65; 
       fprintf(ofp, "%c", tolower(c)); 
      } 
      else 
      { 
       int c = ((((buffer[i] - key[j]) + 26)) % 26) + 65; 
       fprintf(ofp, "%c", tolower(c)); 
      } 
     } 
     else if (buffer[i] >= 97 && buffer[i] < 123) 
     { 
      int c = ((((buffer[i] - 97) - ((key[j] - 65) % 26))) % 26) + 97; 

      fprintf(ofp, "%c", tolower(c)); 

     } 
     else 
     { 
      fprintf(ofp, "%c", buffer[i]); 
      continue; 
     } 
     j++; 
    } 
    printf("\n"); 

    fclose(ifp); 
    fclose(ofp); 

    return 0; 
} 

void main() 
{ 
    int ch; 
    char key[20]; 
    a:printf("0.Exit the Menu\n1.Encrypt\n2.Decrypt\n"); 
    printf("Enter your choice\n"); 
    scanf("%d", &ch); 


    switch (ch) 
    { 

    case 0: 
     printf("Goodbye\n"); 
     break; 

    case 1: 
     printf 
      ("-----------------------------Welcome to the encryption zone---------------------\n"); 
     printf("Enter the key to be used\n"); 
     scanf("%s", key); 

     Encrypt(key); 
     break; 

    case 2: 
     printf 
      ("-----------------------------Welcome to the decryption zone---------------------\n"); 
     printf("Enter the key to be used\n"); 
     scanf("%s", key); 

     Decrypt(key); 
     break; 

    default: 
     printf("Enter the correct choice\n"); 
     goto a; 
     break; 

    } 
} 
+2

어둠에있는 샷 : 널 종료하는 것을 잊었습니까? –

+0

이 경우'goto'를 사용하지 마십시오. 루프를 사용하십시오. –

+0

그리고 정말로 두 번째와 세 번째 인수의 위치를'fread'와'fwrite'로 바꾸어야합니다. 두 번째는 각 요소의 크기이고 세 번째 요소는 요소의 수입니다. 물론 반환 값의 검사도 변경해야합니다. –

답변

3

당신이 할당하고 여기에 복사

buffer = (char *)malloc(sizeof(char) * sz); 
if (!buffer) 
    fclose(ifp), fputs("memory alloc fails", stderr), exit(1); 

/* copy the file into the buffer */ 
if (1 != fread(buffer, sz, 1, ifp)) 
    fclose(ifp), free(buffer), fputs("entire read fails", stderr), exit(1); 

당신이 버퍼에 sz 바이트를 할당하고, sz 바이트를 복사 한 경우,하지만 당신은하지 않았다 널 종결자를위한 공간을 남겨 두십시오. 따라서 strlen(buffer)을 확인하고 나중에 암호화하면 할당하지 않은 메모리로 이동하게됩니다.

이 문제를 해결하려면 '\0'에 하나의 여분의 바이트를 할당해야합니다. 그렇지 않으면 끝에있는 '\0'을 1 개 적게 복사하고 압정합니다.

+0

우분투 (gcc 컴파일러)에서 문제가 발생하지 않았지만 Windows의 코드 블록에서 컴파일 할 때 발생합니다. –

+1

할당되지 않은 메모리로 실행되면 즉시 정의되지 않은 동작이 발생합니다. 'valgrind'와 같은 메모리 검사기로 프로그램을 실행 해 보셨습니까? 메모리 오류가 발생하면 문제가 없다는 것이 아니라 운이 좋다는 것과 정의되지 않은 동작이 충돌을 일으키지 않는 것입니다. –

0

길이 계산이 잘못되었습니다.

@Dennis Meng이 언급 한대로 strlen (버퍼) 가능성이 (* 1)은 할당 된 버퍼 끝을지나갑니다. malloc()은 문제가 없습니다. 권장되는 해결 방법은 다릅니다. 오히려 NUL의 문자에 시침보다 당신이 더 이상 수행 strlen(buffer)sz 배로 더 빠르게 실행되지 않습니다,뿐만 아니라이 문제가 해결됩니다

for (i = 0; i < sz; i++) 

2 개 for 루프

for (i = 0; i < strlen(buffer); i++) 

에 대한 변경합니다.

* 1 파일에 NUL 문자가 포함되어 있어도 별 도움이되지 않습니다. 그것이 buffer의 끝을 지나면, 일들이 멈추는 곳은 UB입니다.

관련 문제