Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program name encrypt - encrypt or decrypt a file using DES encryption Usage encr

ID: 3887490 • Letter: P

Question

Program name

encrypt - encrypt or decrypt a file using DES encryption

Usage

encrypt <key> <input file> <output file> <mode>

key: an 8 character string used as the DES key to encrypt/decrypt

input file: the file to encrypt/decrypt

output file: the result of the operation

mode: specifies whether to encrypt or decrypt- if mode=0, then encrypt the input file, if mode=1 then decrypt the input file

Can you please help in writing a program that takes a test file and encrypts and decrypts and write to an output file in C PROGRAM

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>

int encryptDES(void);
int decryptDES(void);
int encryptDES_view(void);
int decryptDES_view(void);

FILE *fileP1, *fileP2;
char ch;

int main()
{
int choice;
while(1)
{   
printf("Select One of the Following: ");
printf(" 1. encryptDES ");   
printf("2. decryptDES ");
printf("3. View The Encypted File here ");
printf("4. View The Decrypted File here ");
printf("5. Exit ");
printf(" Enter Your Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1: encryptDES();
break;
case 2: decryptDES();
break;
case 3: encryptDES_view();
break;
case 4: decryptDES_view();
break;
case 5: exit(1);
}
}
printf(" ");
return 0;
}

int encryptDES()
{
printf(" ");
fileP1 = fopen("/home/Desktop/Source","r");
if(fileP1 == NULL)
{
printf("Source File Could Not Be Found ");
}
fileP2 = fopen("/home/Desktop/Target","w");
if(fileP2 == NULL)
{
printf("Target File Could Not Be Found ");
}
while(1)
{
ch = fgetc(fileP1);
if(ch == EOF)
{
printf(" End Of File ");
break;
}
else
{
ch = ch - (8 * 5 - 3);
fputc(ch, fileP2);
}
}
fclose(fileP1);
fclose(fileP2);
printf(" ");
return 0;
}

int decryptDES()
{
printf(" ");
fileP1 = fopen("/home/Desktop/Target","r");
if(fileP1 == NULL)
{
printf("Source File Could Not Be Found ");
}
fileP2 = fopen("/home/Desktop/Source","w");
if(fileP2 == NULL)
{
printf("Target File Could Not Be Found ");
}
while(1)
{
ch = fgetc(fileP1);
if(ch == EOF)
{
printf(" End Of File ");
break;
}
else
{
ch = ch + (8 * 5 - 3);
fputc(ch, fileP2);
}
}
fclose(fileP1);
fclose(fileP2);
printf(" ");
return 0;
}

int encryptDES_view()
{
printf(" ");
fileP1 = fopen("/home/Desktop/Target","r");
if(fileP1 == NULL)
{
printf("No File Found ");
exit(1);
}
else
{
while(1)
{
ch = fgetc(fileP1);
if(ch == EOF)
{
break;
}
else
{
printf("%c", ch);
}
}
printf(" ");
fclose(fileP1);
}
printf(" ");
return 0;
}

int decryptDES_view()
{
printf(" ");
fileP1 = fopen("/home/Desktop/Source","r");
if(fileP1 == NULL)
{
printf("No File Found ");
exit(1);
}
else
{
while(1)
{
ch = fgetc(fileP1);
if(ch == EOF)
{
break;
}
else
{
printf("%c", ch);
}
}
printf(" ");
fclose(fileP1);
}
return 0;
printf(" ");
}