-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypting_or_decrypting_file.c
57 lines (57 loc) · 1.2 KB
/
encrypting_or_decrypting_file.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void encrypt(int size,char arr[]){
int i;
FILE *fp=fopen("files\\data.txt","r+");
fgets(arr,size,fp);
for(i=0;i<strlen(arr);i++)
arr[i]+=2;
rewind(fp);
fputs(arr,fp);
fclose(fp);
printf("\nEncyrption Successful!");
}
void decrypt(int size,char arr[]){
int i;
FILE *fp=fopen("files\\data.txt","r+");
fgets(arr,size,fp);
for(i=0;i<strlen(arr);i++)
arr[i]-=2;
rewind(fp);
fputs(arr,fp);
fclose(fp);
printf("\nDecyrption Successful!");
}
int calc_file_size(){
char read; int size=0;
FILE *fp=fopen("files\\data.txt","r");
if(!fp){
printf("Error in opening file!!\a");
exit(1);
}
while(!feof(fp)){
fscanf(fp,"%c",&read);
size++;
}
fclose(fp);
return size;
}
void main(){
int choice,size=calc_file_size();
char arr[size];
while(1){
system("cls");
fflush(stdin);
printf("Note: U must type a paragrapgh in 'data.txt' file fisrt,in order to utilize this program!\n");
printf("Select:\n1. Encryption\n2. Decryption\n==>");
scanf("%d",&choice);
if (choice>0&&choice<3&&choice!=32761)
break;
printf("\a");
}
if(choice==1)
encrypt(size,arr);
else if(choice==2)
decrypt(size,arr);
}