-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileEncryption.c
51 lines (42 loc) · 1.27 KB
/
FileEncryption.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
/*******************************************************************************
author : Alex Szpakiewicz
description:
*******************************************************************************/
#include "project_utils.h"
#include<stdio.h>/*printf,scanf,fopen,fclose,fgetc,fputc*/
#define OUTPUT_FILENAME "Ascii_Encryption.txt"/* constant-output file name*/
#define MAX_NAME_SIZE 80/*constant-The maximum size of name*/
void encryptFile()
{
FILE *f1=NULL;
FILE *f2=NULL;
int key;
char c;
char fileName[MAX_NAME_SIZE];
printf("Enter the name of the file you want to encrypt:\n");
scanf("%s",name);
/*Get the file name from the user
and input to the function to open the file*/
f1 = fopen(name, "r");
if( f1 == NULL)
printf("File not found\n");
return;
}
printf("Input the key which is a number between 1 and 93:\n");
scanf("%d",&key);
while(key<0||key>93)
{
printf("Invalid key, please input a number between 1 and 93:\n");
printf("Input a new key:");
scanf("%d",&key);
}
f2 = fopen(OUTPUT_FILENAME, "w");
while((c=fgetc(f1))!=EOF)
{
c=(c-32+key)%94+32;
fputc(c,f2);
}
fclose(f2);
fclose(f1);
printf("The file is encrypted!\n");
}