Question

In: Computer Science

Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...

Programming Language C++

Encrypt a text file using Caesar Cipher.

Perform the following operations:

  • Read the console input and create a file. ['$' character denotes end of content in file.]
  • Close the file after creation.
  • Now encrypt the text file using Caesar Cipher (Use key value as 5).
  • Display the contents of the updated file.

#include <iostream>

using namespace std;

int main() {

// write code here

}

Solutions

Expert Solution

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
void encrypt(char *text, int s)
{
   FILE *fptr;
   fptr = fopen("program.txt", "w+");
   int len = strlen(text);
char result[len+1];
int j=0 ;
for (int i=0;i<strlen(text);i++)
{
if (isupper(text[i]))
result[j]= char(int(text[i]+s-65)%26 +65);
  
       else if(islower(text[i]))
           result[j]= char(int(text[i]+s-97)%26 +97);
else
           result[j]=text[i];
j++;
}
   result[j]='\0';
fprintf(fptr,"%s",result);
fclose(fptr);
}
int main()
{
   char buffer[1000];
   char buff[1000];
FILE *fptr;
fptr = fopen("program.txt", "w+");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter the text to be tested for cipher use $ as last character and press enter to end\n");
fgets(buffer, sizeof(buffer), stdin);
fprintf(fptr,"%s", buffer);
fclose(fptr);
fptr = fopen("program.txt", "r");
char c;
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
c = fgetc(fptr);
int i=0;
while (c!='$'&&c != EOF)
{
buffer[i]=c;
c = fgetc(fptr);
i++;
}
buffer[i]='\0';
fclose(fptr);
encrypt(buffer, 5);
fptr = fopen("program.txt", "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
cout<<"File content is"<<endl;
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}

   return 0;
}


Related Solutions

Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Binary file IO Suppose a file has been encrypted using the Caesar cipher as described above,...
Binary file IO Suppose a file has been encrypted using the Caesar cipher as described above, and you know the secret key. Write a program to decrypt (or decode) the file. Your program will prompt the user to enter an input file name for the encrypted file, an output file name for the unencrypted version of the input file, and the secret key. Create a DataInputStream for the input file and a DataOutputStreams for the output file. Next, read the...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Using playfair cipher, encrypt the plaintext “SUCCESS” using the keyword “MIDTERMEXAM”
Using playfair cipher, encrypt the plaintext “SUCCESS” using the keyword “MIDTERMEXAM”
Systems Programming - File Operations in Linux using C Preventing copying the same file What does...
Systems Programming - File Operations in Linux using C Preventing copying the same file What does the standard cp do if you try to copy a file onto itself? $ cp file1 file1 cp: 'file1' and 'file1' are the same file Modify cp1.c to handle the situation and include comments. /** * @file cp1.c * @brief Uses read and write with tunable buffer size * usage: cp1 src dest */ #include <stdio.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define BUFFERSIZE...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT