Question

Program 1: For this program you will be implementing a simple rotation encryption to be used...

Program 1: For this program you will be implementing a simple rotation encryption to be used on a text file. Your program will need to read in text from a text file and encrypt or decrypt it (have the user select which) using the encryption key entered by the user, then write the results out to another file. The user will enter the names of the source and destination files, the encryption key, and will select whether they are encrypting or decrypting the file. You can use any text file of choice.

The program should have these 4 functions:

  • A function that reads from a file (takes a single string parameter for the filename and returns a string).
  • A function that writes to a file (takes 2 string parameters, one for the filename and one for the contents to be written, return type of void).
  • A function that performs encryption (takes a string parameter for the string to be encrypted and an int parameter for the encryption key, returns the encrypted result as a string).
  • A function that performs decryption (takes a string parameter for the string to be encrypted and an int parameter for the encryption key, returns the decrypted result as a string).
  • Hint: Encryption and decryption are very similar – you might want to have your encryption function call your decryption function or vice versa, or write a third function that encryption and decryption both call in order to avoid repetition.
  • Note: The Main should handle all user input. The encrypted or decrypted result is written to a file. Acceptable encryption keys are in the range 1-25 inclusive, you must validate the user’s entered encryption key and if it is out of range require that they re-enter it.

Homework Answers

Answer #1

C CODE WITH ALL FOUR FUNCTIONS :-

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
encrypt();
decrypt();
read();
write();
}
void read()
{
   char c[1000];
   FILE *fp;
   if((fp=fopen("filename.txt", "r"))==NULL)
   {
       printf("Error in opening file..!!");
       getch();
       exit(1);
   }
   fscanf(fp, "%s", c);
   printf("Data in file = %s",c);
   fclose(fp);
   getch();
}
void write()
{
   FILE *fp;
   char s[100], fname[20];
   printf("Enter a file name with extension (like file.txt) to create a file : ");
   gets(fname);
   fp=fopen(fname, "w");
   if(fp==NULL)
   {
       printf("Error in opening file..!!");
       getch();
       exit(1);
   }
   printf("\nEnter few lines of text :\n");
   while(strlen(gets(s))>0)
   {
       fputs(s, fp);
       fputs("\n",fp);
   }
   fclose(fp);
   getch();
}
void encrypt()
{
   char fname[20], ch, choice;
   FILE *fps, *fpt;
   printf("Enter file name (with extension like file.txt) to encrypt : ");
   gets(fname);
   fps=fopen(fname, "r");
   if(fps==NULL)
   {
       printf("Error in opening file..!!");
       printf("\nPress any key to exit...");
       getch();
       exit(1);
   }
   fpt=fopen("temp.txt", "w");
   if(fpt==NULL)
   {
       printf("Error in creating temp.txt file..!!");
       fclose(fps);
       printf("\nPress any key to exit...");
       getch();
       exit(2);
   }
   while(1)
   {
       ch=fgetc(fps);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           ch=ch+100;
           fputc(ch, fpt);
       }
   }
   fclose(fps);
   fclose(fpt);
   fps=fopen(fname, "w");
   if(fps==NULL)
   {
       printf("Error in opening source file..!!");
       printf("\nPress any key to exit...");
       getch();
       exit(3);
   }
   fpt=fopen("temp.txt", "r");
   if(fpt==NULL)
   {
       printf("Error in opening temp.txt file...!!");
       fclose(fps);
       printf("\nPress any key to exit...");
       getch();
       exit(4);
   }
   while(1)
   {
       ch=fgetc(fpt);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           fputc(ch, fps);
       }
   }
   printf("File %s encrypted successfully..!!", fname);
   printf("\nPress any key to exit...");
   fclose(fps);
   fclose(fpt);
   getch();
}

void decrypt()
{
   char ch, choice, fname[20];
   FILE *fps, *fpt;
   printf("Enter file name (with extension like file.txt) which you have encrypted earlier to decrypt : ");
   gets(fname);
   fps=fopen(fname, "w");
   if(fps==NULL)
   {
       printf("Error in opening source file..!!");
       printf("\nPress any key to exit...");
       getch();
       exit(7);
   }
   fpt=fopen("temp.txt", "r");
   if(fpt==NULL)
   {
       printf("Error in opening temp.txt file..!!");
       fclose(fps);
       printf("\nPress any key to exit...");
       getch();
       exit(9);
   }
   while(1)
   {
       ch=fgetc(fpt);
       if(ch==EOF)
       {
           break;
       }
       else
       {
           ch=ch-100;
           fputc(ch, fps);
       }
   }
   printf("File %s decrypted successfully..!!",fname);
   printf("\nPress any key to exit...");
   fclose(fps);
   fclose(fpt);
   getch();
}

***GENTLE REMINDER:-PLEASE GIVE GOOD RATINGS.***

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret code so that only authorised entities can read it. Encrypting data is considered a very effective way of achieving data security. To access encrypted data, you must have access to a secret key that enables you to decrypt it. Unencrypted data is called plain text; encrypted data is referred to as cipher text. There are two types of encryption: • Symmetric encryption • Asymmetric...
Please answer this question using python programming only and provide a screen short for the code....
Please answer this question using python programming only and provide a screen short for the code. Reading: P4E 7; Tut 7.2, 7.2.1 Upload an original Python script that satisfies the following criteria: It has at least two functionsFunction 1: This function has one parameter, a string containing the path to an existing text file that will be read by your function Using a with statement, the function opens the file indicated by the parameter for reading The function counts the...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a...
IN MIPS ASSEMBLY Macro File: Create macros to print an int, print a char, print a string, get a string from the user, open file, close file, read file, and allocate heap memory. You can use more macros than these if you like. Main Program File: Allocate 1024 bytes of dynamic memory and save the pointer to the area. The main program is a loop in which you ask the user for a filename. If they enter nothing for the...
Please program in JAVA: Program each option such as delete line and such. Your program will...
Please program in JAVA: Program each option such as delete line and such. Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as: Menu: m Delete line:...
Write a program in Java that allows users to decrypt files. The file encryption technique that...
Write a program in Java that allows users to decrypt files. The file encryption technique that was used to encrypt the file is a Caesar cipher. A Caesar Cipher works by shifting all the letters in the file by a certain amount. So for a file with an encryption key of 1 an “a” will be represented by a “b”, and a “b” will be represented by a “c” in the encrypted file. With an encryption key of 2, an...