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

C Programming: Problem Description: Suppose an encrypted file was created using

ID: 3764115 • Letter: C

Question

C Programming:

Problem Description: Suppose an encrypted file was created using the encoding/decoding scheme . Each letter is substituted by some other letter according to a given mapping as shown below. char * letters = "abcdefghijklmnopqrstuvwxyz"; char * enc = "kngcadsxbvfhjtiumylzqropwe"; For example, every 'a' becomes a 'k' when encoding a text, and every 'k' becomes an 'a' when decoding. You will write a program, encode or decode a File, and then encodes or decodes the File using the mapping above. Capital letters are mapped the same way as the lower case letters above, but remain capitalized. For example, every 'A' becomes 'K' when encoding a file, and every 'K' becomes an 'A' when decoding. Numbers and other characters are not encoded and remain the same. Write a program to read a file and encode the file to an encrypted file. And write a program to get an encrypted file and decode to original file. Your program should prompt the user to enter an input file name and an output file name What should you do? 1. Ask for input file name/ output file name (encrypted file). The encrypt using above 2. Ask for encrypted file and decoded to original input file. encode/decode.

Explanation / Answer

Program code:

// encryption.cpp : Deinfilenes the entry point for the console application.

//

#include "stdafx.h"

#include<iostream>

#include <stdio.h>

#include <stdlib.h>

#include<string.h>

using namespace std;

//encryption method definition:

char getEncryption(char *out, char *encryption, char x)

{

int i = 0;

while(out[i] != ''){

if(out[i] == x) return encryption[i];

i++;

}

return x;

}

//decryption method definition:

char getDecryption(char *out, char *encryption, char x){

int i = 0;

while(encryption[i] != ''){

if(encryption[i] == x) return out[i];

i++;

}

return x;

}

int main(){

char *alphabets = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//input message variable

char *encryption = "kngcadsxbvfhjtiumylzqropweKNGCADSXBVFHJTIUMYLZQROPWE";//decryption code varible

char input[20], output[20];

int encORdec;

printf("Enter your choice:");

printf("for Encryption(1) and for Decryption(2)");

scanf("%d", &encORdec);

printf("Enter the input infilele name: ");

scanf("%s", input);

FILE *infile = fopen(input, "r");

printf("Enter the output infilele name: ");

scanf("%s", output);

FILE *outfile = fopen(output, "w");

char tempVar=' ';

while(fscanf(infile, "%c", &tempVar) != EOF)//reading from file

{

if(encORdec == 1) fprintf(outfile, "%c", getEncryption(alphabets, encryption, tempVar));

else fprintf(outfile, "%c", getDecryption(alphabets, encryption, tempVar));

}

printf("See the output infilele %s ", output);

return 0;

}

Sample output:

inputfile.txt

after decryption: