Я целый день пытался расшифровать файл в c. Мне нужно написать программу, которая может шифровать и дешифровать файлы. У меня есть файл bin, который мне нужно расшифровать и получить от него текст. Это то, что я сделал сейчас, поскольку я вижу, что это только шифрует. Помоги мне, пожалуйста.Пожалуйста, помогите мне с расшифровкой в C
Вот мой код:
#include <stdio.h>
#include <string.h>
#define MAX 256
int main() {
FILE *fp1, *fp2;
int key, val, ch, i = 0;
char filename[MAX], temp[] = "temp.txt";
/* get the file name from the user */
printf("Enter your file name:");
scanf("%s", filename);
/* open the given file in read mode */
fp1 = fopen(filename, "rb");
/* error handling */
if (!fp1) {
printf("Unable to open the input file!!\n");
return 0;
}
/* open the temporary file in write mode */
fp2 = fopen(temp, "wb+");
/* error handling */
if (!fp2) {
printf("Unable to open the temporary file!!\n");
return 0;
}
/* get the key from the user to create cipher */
printf("Enter the key to create cipher text:");
scanf("%d", &key);
/* converting plain text to cipher text */
while ((ch = fgetc(fp1)) != EOF) {
/* adding key to the ascii value of the fetched char */
val = (ch + key)%256;
fprintf(fp2, "%d ", val);
i++;
if (i % 10 == 0) {
fprintf(fp2, "\n");
}
}
/* closing all opened files */
fclose(fp1);
fclose(fp2);
/* open the file containint cipher text in read mode */
fp2 = fopen(temp, "r");
/* printing the cipher text */
printf("\nCipher text for the given plain text:\n");
while (!feof(fp2)) {
fscanf(fp2, "%d", &ch);
if (!feof(fp2)) {
printf("%c", ch);
}
}
/* moving the file pointer to the start of file */
rewind(fp2);
/*
* converting the cipher text to plain
* text and printing it to console
*/
printf("\n\nConverting the cipher text to plain text:\n");
while (!feof(fp2)) {
fscanf(fp2, "%d", &ch);
if (!feof(fp2)) {
val = (ch - key)%256;
printf("%c", val);
}
}
printf("\n");
/* close all opened files */
fclose(fp2);
return 0;
printf("\n");
}
А вот выход:
121 164 152 154 129 133 130 136 94 121
23 248 23 16 23 10 23 243 94 134
116 132 116 195 182 190 94 144 144 131
160 185 194 187 200 188 116 135 116 132
116 166 131 154 189 192 200 185 198 131
154 192 181 200 185 152 185 183 195 184
185 146 146 94 199 200 198 185 181 193
94 204 240 65 111 29 254 127 15 197
211 18 22 63 20 197 120 233 218 194
132 90 163 123 228 49 159 98 184 101
6 31 84 117 93 56 193 70 79 253
126 161 249 194 157 10 67 119 15 196
13 146 193 9 218 238 155 173 113 73
53 147 115 83 146 124 80 59 9 141
216 41 112 210 82 31 27 115 210 201
76 171 112 145 174 89 140 250 226 26
214 218 23 35 211 79 12 210 211 108
89 155 207 92 154 113 53 68 81 59
23 3 19 72 149 79 23 75 179 83
204 166 78 96 123 185 34 243 58 248
212 147 1 198 30 255 244 234 135 210
173 69 35 123 248 173 69 69 22 7
2 59 57 248 194 80 204 163 87 76
69 164 179 72 59 63 80 189 163 174
53 247 42 189 30 243 18 211 79 69
76 82 76 221 212 144 14 89 181 24
19 181 173 96 100 252 181 5 109 168
Но он должен быть текст, а не числа
Посмотрите [здесь] (http://www.asciitable.com/) и убедитесь, что ваши зашифрованные символы остаются в пределах диапазона печатаемых, то есть имеют код ASCII между [32, 127]. – vsoftco
«Расшифровка» примерно такая же описательная, как и молоко. И никто не использует молоко для описания вещей. Закрывая это. – Blindy
Поскольку вы, кажется, печатаете с помощью 'fprintf (fp2,"% d ", val);', ясно, что вы хотите напечатать цифры. Если вы хотите печатать символы, вы будете использовать '% c', а не'% d'. –