Create a C-program thats reads in a text-file(specified by the user, prefors a \
ID: 3848589 • Letter: C
Question
Create a C-program thats reads in a text-file(specified by the user, prefors a "bit-level Exclusive-OR" on each byte of the text-file (as a method of encryption, then writes the encrypted file data back out to disk and gives the encryptd file the same name as the input file (overwrites the original file)
Application- Asks user to enter name of file to be encrypted
User- Enters name of file
Application- Encryps each bye of the input file and writes encrypted data back to the same file, over writing the orginal data.
Thank you!
#includeExplanation / Answer
#include <stdio.h>
FILE *finp;
FILE *keyFile;
main()
{
String file;
printf("Enter file name ");
scanf("%s", &file);
finp=&file;
if ( strcmp(argc[1], "e") == 0 )
{
if ( (finp = fopen(argc[2],"r")) == NULL )
{
printf("Could Not Open file %s ", argc[2]);
exit(1);
}
if ( (keyFile = fopen(argc[3],"r")) == NULL )
{
printf("Could Not Open file %s ", argc[3]);
exit(1);
}
}
char encrypt(char a, char b)
{
int aa = index(a & 255);
int bb = index(b & 255);
int cc = (aa + bb) % 191;
return unindex(cc);
}
char decrypt (char a, char b){
int aa = index(a & 255);
int bb = index(b & 255);
int cc = (191 + aa - bb) % 191;
return unindex(cc);
}
char unindex(int i)
{
if (i <= 94) {
return (char)(i + 32);
} else {
return (char)(i + 65);
}
}
int index(int c)
{
int i;
if ((c >= 32) && (c <= 126)) {
i = c - 32;
} else if ((c >= 160) && (c <= 255)) {
i = c - 65;
} else {
i = 63; // Question mark
}
return i;
}