31.5k views
4 votes
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

User Yick Leung
by
5.5k points

2 Answers

5 votes

Answer:

C code is given below

Step-by-step 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.\\");

return 1;

}

// try to read the key file

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

if( !key ) {

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

return 1;

}

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

if( !data ) {

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

return 1;

}

// Store key as string and get length

int kLen = strlen(key);

int dataLen = strlen( data );

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

printf("%s\\", 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("\\");

}

}

printf("\\");

return 0;

}

User Mnwsmit
by
5.3k points
3 votes

Answer:

code is written in c++ below

Step-by-step 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 << "\\";

cout << "Original/Decrypted Text : "

<< originalText(cipher_text, key);

return 0;

}

User RandallB
by
4.9k points