나는 vigenere 암호를 사용하여 메시지를 암호화하고 암호 해독하는 프로그램을 작성했습니다. 암호화 부분을 잘 작동, 문제는 해독 부분입니다 : char * * free() 내 프로그램 충돌을 사용하여 할당 취소 할 때. 할당을 해제하지 않으면 프로그램이 제대로 작동하지만 충돌이 발생하지 않습니다.메모리를 할당 해제하기위한 free 명령을 호출하면 프로그램이 중단됩니다. 왜?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int main(void) {
int choice, check, error;
char ch;
printf("What do you want to do?\n"
"1...Decode a message that is saved in a ciphertext.txt file.\n"
"2...Encode a message and save it in a ciphertext.txt file.\n");
do {
error = 0;
check = scanf("%d%c", &choice, &ch);
if (check != 2 || ch != '\n' || choice < 1 || choice > 2) {
printf("Error: Invalid input!\n");
error = 1;
}
fflush(stdin);
} while (error);
if (choice == 2) {
FILE *fp = fopen("ciphertext.txt", "w+");
char buffer[1000];
char *p, *q, *r;
char *encryption_text;
char *encrypted;
char *decrypted;
printf("What are bad characters?\n");
printf("-Everything except A-Z and a-z.\n");
printf("Plaintext: ");
gets(buffer);
decrypted = (char *)malloc(strlen(buffer));
strcpy(decrypted, buffer);
printf("Key: ");
gets(buffer);
encryption_text = (char *)malloc(strlen(buffer));
strcpy(encryption_text, buffer);
encrypted = (char *)malloc(strlen(decrypted));
strcpy(encrypted, decrypted);
putchar('\n');
p = decrypted;
q = encryption_text;
r = encrypted;
if (strlen(q) > strlen(p)) {
printf("The key has to be shorter or equal length as the plaintext.");
return 0;
}
while (*p != '\0') {
if (!(*p >= 'A' && *p <= 'Z') && !(*p >= 'a' && *p <= 'z')) {
printf("Bad characters.");
return 0;
}
if (*q == '\0') {
q = encryption_text;
} else if (!(*q >= 'A' && *q <= 'Z') && !(*q >= 'a' && *q <= 'z')) {
printf("Bad characters.");
return 0;
}
if (*p >= 'a' && *p <= 'z') *p -= ' ';
if (*q >= 'a' && *q <= 'z') *q -= ' ';
*r = (*p - 'A' + *q - 'A') % 26 + 'A';
p++;
q++;
r++;
}
printf("Ciphertext: ");
puts(encrypted);
fputs(encryption_text, fp);
fprintf(fp, ";");
fputs(encrypted, fp);
free(decrypted);
free(encrypted);
free(encryption_text);
fclose(fp);
} else {
FILE *fp = fopen("ciphertext.txt", "r+");
char buffer[1000];
char *encryption_text;
char *encrypted;
char *decrypted;
char delimeter[2] = ";";
char *token;
char *p, *q, *r;
fgets(buffer, 1000, fp);
encrypted = (char *)calloc(strlen(buffer) * sizeof(char), sizeof(char));
encryption_text = (char *)calloc(strlen(buffer) * sizeof(char), sizeof(char));
token = strtok(buffer, delimeter);
strcpy(encryption_text, token);
encrypted = strrchr(buffer, '\0');
if (encrypted != NULL) {
strcpy(encrypted, encrypted + 1);
}
decrypted = (char *)calloc(strlen(buffer) * sizeof(char), sizeof(char));
for (p = decrypted, q = encryption_text, r = encrypted;
*r != '\0'; p++, r++, q++) {
if (*q == '\0') {
q = encryption_text;
}
*p = (*r - 'A' - (*q - 'A') + 26) % 26 + 'A';
}
printf("Plaintext: ");
puts(decrypted);
printf("Key: ");
puts(encryption_text);
printf("Ciphertext: ");
puts(encrypted);
if (decrypted) free(decrypted);
if (encrypted) free(encrypted);
if (encryption_text) free(encryption_text);
fclose(fp);
}
return 0;
}
나는 그것이 충돌 이유를 말해 줄 수 있기를 바랍니다 : 다음은 프로그램 코드입니다. 감사!
'해독 = (숯 *)의 malloc (나 strlen (완충액))'-> ''해독 = (숯 *)의 malloc (나 strlen (버퍼) +1)'모든 대 – BLUEPIXY
동일 다른'malloc' 호출. NUL 종결 자 문자열을위한 공간을 할당하지 않습니다. 메모리 손상이 발생하여 정의되지 않은 동작 (임의의 시점에서의 충돌 포함)이 발생합니다. – kaylum
그러나 버퍼에는 1000 자의 공백이 있으며 해독 된 3 개의 암호화 된 모든 encryption_text는 버퍼보다 짧습니다. 그렇지 않니? –