코드에 두 개의 파일이 있어야합니다. code.c는 stdin에서 디코딩하는 코드와 decode.c를 인코딩합니다. 각 줄의 길이를 120 자 이하로 가정하면 길이 파일 크기에 문제가 없어야합니다.이 C 코드가 메시지를 인코딩/디코딩하는 데있어 잘못된 점
내 인코딩 코드 ...
void encode(char pass[]) {
char buffer[121], encoded[121];
int i = 0, j = 0;
while (fgets(buffer, 120, stdin)) {
for (i = 0; i < (strlen(buffer)); i++) {
if (j > (strlen(pass) - 1))
j = 0;
encoded[i] = buffer[i] + pass[j];
j++;
}
fprintf(stdout, encoded);
}
fprintf(stderr, buffer);
}
그리고 정확히 반대, 디코드 코드 :
void decode(char pass[]) {
char buffer[121], decoded[121];
int i = 0, j = 0;
while (fgets(buffer, 120, stdin)) {
for (i = 0; i < (strlen(buffer)); i++) {
if (j > (strlen(pass) - 1))
j = 0;
decoded[i] = buffer[i] - pass[j];
j++;
}
fprintf(stdout, decoded);
}
}
코드가 인코딩 것으로 보인다 것을 제외하고, 실행하고 의도 한대로 거의 작동하는 것 같다 CTRL + D를 눌러 종료 할 때 마지막 행에 더 많은 데이터가 있습니다. 그래서 디코딩하려고 할 때 마지막 줄을 제외하고 제대로 디코딩됩니다.
편집 : 여기
는 '암호'파일의 암호를 사용하여 파일 이름 토굴에
This is a test
file that will contain
encrypted information
을 넣어해야합니다 몇 가지 예제 입력
code password > crypt
This is a test
file that will contain
encrypted information
입니다 토굴은 다음과 같습니다 :
01 23,516,ÄÉÜæ<97>Øå<84>Ñ<81>çØêãÊÙÍØ<93>ë×ÓØ<90>ØÜßã<8f>ÕÓÞÕÔÜåy×ÒÓÓìãëÔÖ<84>ÙÏÙâéÜÓØÙÐá}y
복호화 출력은 그러나 같습니다
decode password < crypt
This is a test
file that will contain
encrypted information
??
을 ?? 파일에서 가져 오는 가비지 텍스트입니다. 표준 입력에 입력 ..
입력의 또 다른 라운드가 인코딩 또는 디코딩 중이었다보다 더 이유는 확실하지/출력이 수행합니다
./code password > crypt
testing this
file to encrypt
stuff
./decode password < crypt
testing this
file to encrypt
!J
내 마지막 줄에서 일어나는 문제가 훨씬 더 분명 반면 .
질문에 실제 출력 예제와 일부 예제 입력에 대한 예상 출력을 추가 할 수 있습니까? –
일부 입력을 추가했습니다. – MaxLo