1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Maksim231197 [3]
3 years ago
12

In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program

will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below. Command Line Parameters 1. Your program must compile and run from the command line. 2. Input the required file names as command line parameters. Your program may NOT prompt the user to enter the file names. The first parameter must be the name of the encryption key file, as described below. The second parameter must be the name of the file to be encrypted, as also described below. The sample run command near the end of this document contains an example of how the parameters will be entered. 3. Your program should open the two files, echo the processed input to the screen, make the necessary calculations, and then output the ciphertext to the console (terminal) screen in the format described below. Note: If the plaintext file to be encrypted doesn't have the proper number (512) of alphabetic characters, pad the last block as necessary with the letter 'X'. Make sure
Computers and Technology
2 answers:
skad [1K]3 years ago
5 0

Answer:

code is written in c++ below

Explanation:

// C++ code to implement Vigenere Cipher

#include<bits/stdc++.h>

using namespace std;

// This function generates the key in

// a cyclic manner until it's length isi'nt

// equal to the length of original text

string generateKey(string str, string key)

{

int x = str.size();

for (int i = 0; ; i++)

{

if (x == i)

i = 0;

if (key.size() == str.size())

break;

key.push_back(key[i]);

}

return key;

}

// This function returns the encrypted text

// generated with the help of the key

string cipherText(string str, string key)

{

string cipher_text;

for (int i = 0; i < str.size(); i++)

{

// converting in range 0-25

int x = (str[i] + key[i]) %26;

// convert into alphabets(ASCII)

x += 'A';

cipher_text.push_back(x);

}

return cipher_text;

}

// This function decrypts the encrypted text

// and returns the original text

string originalText(string cipher_text, string key)

{

string orig_text;

for (int i = 0 ; i < cipher_text.size(); i++)

{

// converting in range 0-25

int x = (cipher_text[i] - key[i] + 26) %26;

// convert into alphabets(ASCII)

x += 'A';

orig_text.push_back(x);

}

return orig_text;

}

// Driver program to test the above function

int main()

{

string str = "GEEKSFORGEEKS";

string keyword = "AYUSH";

string key = generateKey(str, keyword);

string cipher_text = cipherText(str, key);

cout << "Ciphertext : "

<< cipher_text << "\n";

cout << "Original/Decrypted Text : "

<< originalText(cipher_text, key);

return 0;

}

solniwko [45]3 years ago
4 0

Answer:

C code is given below

Explanation:

// Vigenere cipher

#include <ctype.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/**

* Reading key file.

*/

char *readFile(char *fileName) {

   FILE *file = fopen(fileName, "r");

   char *code;

   size_t n = 0;

   int c;

   if (file == NULL) return NULL; //could not open file

   code = (char *)malloc(513);

   while ((c = fgetc(file)) != EOF) {

      if( !isalpha(c) )

          continue;

      if( isupper(c) )

          c = tolower(c);

      code[n++] = (char)c;

   }

   code[n] = '\0';

  fclose(file);

   return code;

}

int main(int argc, char ** argv){  

   // Check if correct # of arguments given

   if (argc != 3) {

       printf("Wrong number of arguments. Please try again.\n");

       return 1;

   }

 

  // try to read the key file

  char *key = readFile(argv[1]);

  if( !key ) {

      printf( "Invalid file %s\n", argv[1] );

      return 1;

  }

 

  char *data = readFile(argv[2]);

  if( !data ) {

      printf("Invalid file %s\n", argv[2] );

      return 1;

  }

 

   // Store key as string and get length

   int kLen = strlen(key);

  int dataLen = strlen( data );

 

  printf("%s\n", key );

  printf("%s\n", data );

 

  int paddingLength = dataLen % kLen;

  if( kLen > dataLen ) {

      paddingLength = kLen - dataLen;

  }

  for( int i = 0; i < paddingLength && dataLen + paddingLength <= 512; i++ ) {

      data[ dataLen + i ] = 'x';

  }

 

  dataLen += paddingLength;

 

   // Loop through text

   for (int i = 0, j = 0, n = dataLen; i < n; i++) {          

       // Get key for this letter

       int letterKey = tolower(key[j % kLen]) - 'a';

     

       // Keep case of letter

       if (isupper(data[i])) {

           // Get modulo number and add to appropriate case

           printf("%c", 'A' + (data[i] - 'A' + letterKey) % 26);

         

           // Only increment j when used

           j++;

       }

       else if (islower(data[i])) {

           printf("%c", 'a' + (data[i] - 'a' + letterKey) % 26);

           j++;

       }

       else {

           // return unchanged

           printf("%c", data[i]);

       }

      if( (i+1) % 80 == 0 ) {

          printf("\n");

      }

   }

 

   printf("\n");

 

   return 0;

}

You might be interested in
Which skill type refers to the knowledge and ability to perform a task?
Thepotemich [5.8K]

Sound, Audio for engineering sound Quality's.

7 0
3 years ago
Read 2 more answers
What is the relationship between interrupt and buffer <br>​
Jlenok [28]

Answer:

Operating systems have some code called an 'interrupt handler', which prioritises the interrupts and saves them in a queue. Buffers are used in computers as a temporary memory area, and they are essential in modern computers because hardware devices operate at much slower speeds than the processor.

8 0
2 years ago
This question is for one of my classes I am in right now, and the question is:
suter [353]

Answer:

I will respond by saying the destination is not the most important , the journey is.

Explanation:

8 0
3 years ago
Does anyone know how to move the search bar
jekas [21]

Answer:

I know how to on a phone, it's not the same on a computer. I wish I could help, I have the same problem sometimes :(

Explanation:

8 0
3 years ago
I want to work on cloud computing and i need some help on how to start ?
ddd [48]
A great website that might be able to help is https://www.cybrary.it/

Hope this helps.

6 0
4 years ago
Other questions:
  • In procedural programming, where does the flow of control usually route from the main function?
    8·1 answer
  • Casting is one of the oldest known manufacturing processes. <br> True or false
    6·2 answers
  • Your new home has a vacuum system. what kind of computer is controlling it?
    5·1 answer
  • Technician A says copper has a low resistance. Technician B says the length of wire doesn't affect resistance. Who is correct?
    11·1 answer
  • He memory unit of a computer has 2 20 words. The computer has instruction format with four fields; an operation code field, a mo
    15·1 answer
  • Number of frames displayed per second
    15·1 answer
  • During his last performance review, Franco's boss urged him to set some short-term and long-term sales goals to help him perform
    6·2 answers
  • My PC won't output any data does anyone have any ideas​
    9·1 answer
  • Finding values in an array
    5·1 answer
  • A bookstore owner wants to allow customers to pay for books using their mobile phones. Which version of iOS will support the boo
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!