-
Notifications
You must be signed in to change notification settings - Fork 10
/
file_encrpyt.c
72 lines (61 loc) · 1.83 KB
/
file_encrpyt.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
#include "header.h"
void file_encryption(char key[])
{
char c,sentence[20];
printf("\nDo you want to input string(y/n): ");
scanf("%c",&c);
if(c=='y')
{
printf("\nenter string: ");
scanf("%[^/n]s",sentence);
int keyLen = strlen(key), i, j;
int n= strlen(sentence);
int ciphertext_len = 0;
for (int i = 0;i<n; i++)
{
if ((sentence[i] >= 'A') && (sentence[i] <= '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);
}
else
{
FILE *f1 = fopen("plaintext.txt", "r");
if (f1 == NULL)
printf("error in opening the file");
else
{
char ch;
int keyLen = strlen(key), i, j;
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);
}
}