-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfile_decrypt.c
45 lines (35 loc) · 1.04 KB
/
file_decrypt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <string.h>
#include "header.h"
void file_decryption(char key[])
{
//open the cipher text file
FILE *f1 = fopen("cipher.txt", "r");
//codition to check for error in opening the file
if (f1 == NULL)
printf("error in opening the file");
else
{
char ch;
int keyLen = strlen(key), i, j;
//to find the length of the message in the file
int ciphertext_len = 0;
for (int i = 0; (ch = fgetc(f1)) != EOF; i++)
{
if ((ch >= 'A') && (ch <= 'Z'))
ciphertext_len++;
}
//printf("cipher text length: %d\n", ciphertext_len);
char newKey[ciphertext_len];
//generating new key
for (i = 0, j = 0; i < ciphertext_len; ++i, ++j)
{
if (j == keyLen)
j = 0;
newKey[i] = key[j];
}
newKey[i] = '\0';
printf("\nNew Generated Key: %s\n", newKey);
}
fclose(f1);
}