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

This exercise will implement a ciphering or cryptographic algorithm. Algorithms

ID: 3765975 • Letter: T

Question

This exercise will implement a ciphering or cryptographic algorithm. Algorithms like this are used for
rendering written messages unreadable, at least without some difficulty, except by those who are
possession of “secret” information. The secret information is generally referred to as the “key”.
Making text difficult to read is called enciphering or encrypting. Using the secret key to read the
enciphered message is called deciphering or decrypting. The unencrypted message is called cleartext or
plaintext. The encrypted message is called cryptext or ciphertext.
The idea for this particular algorithm is said to have been due to Julius Caesar so he could send secret
messages to his military commanders. Consequently, the algorithm is called a “Caesar” cipher. Actually,
Caesar cipher is weaker than what you will implement here.
To begin, we need an alphabet. This is a collection of letters, punctuation characters and any other
characters that are allowed to be writing of a cleartext message. These same characters will also be used
to construct the ciphertext, but the characters will be randomly scrambled. The scrambled alphabet with
be out secret key. Here is an example:


Alphabet = abcdefghijklmnopqrstuvwxyz
Key = tslauepmfqirzcxdogyhvbnwj
Notice that the key contains exactly the same characters as the alphabet does, neither more nor less.
The key is simply a re-ordering of the alphabet, Now, let’s use the key to encrpt a simple cleartext
message constructed from the alphabet.
Cleartext = shortmessage
Cryptext = ymxghzuyytpu


Note how the crypt message is constructed. Imagine the alphabet and the key are stored in arrays. We
find the index, 18, of the first cleartext character (‘s’ in the alphabet). Then we create the cryptext
character “y’ by using the character with index 18 in the key array.

For each character in the cleartext:
Find the index i, of the character in the alphabet
Use that same index to get the corresponding character from the key
Use that character for the one in the cleartext.

Now, to decrypt the cryptext and recover the cleartext, we use exactly the same process as that used
above. But, we decrypt by encrypting the cryptext. And we use the key array for our alphabet and the
alphabet array for our key.

Another thing to note is our alphabet can consist of any legitimate characters, including the space,
character, uppercase, lowercase and punctuation characters.
What happens if we encounter a character not in the alphabet, simply print a message to the screen.
Your task is to implement this algorithm using an object-oriented design. Then allow the user to enter
cleartext messages to encrypt and encrypted messages to decipher.

Explanation / Answer

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

class cryptography

{

      public: void encrypt(char *inp,char *out,char alpha[26],char key[26]);

             void decrypt(char *inp,char *out,char alpha[26],char key[26]);

             void readText(char *inp);

};

void cryptography::encrypt(char *inp, alpha[26],char key[26])

{

     ifstream input;

     ofstream output;

     char buf;

     input.open(inp);

     output.open(out);

     buf=input.get();

     while(!input.eof())

     {

         if(buf>='a'&&buf<='z')

         {

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

            {

             if(buf==alpha[i])

             output.put(key[i]);

                       

         }                   

         else

         {

         cout<<”it reads only alphabet”;

         }

          buf=input.get();

     }

     input.close();

     output.close();

     readText(inp);

     readText(out);

}

void cryptography::decrypt(char *inp,char *out, alpha[26],char key[26])

{

     ifstream input;

     ofstream output;

     char buf;

     input.open(inp);

     output.open(out);

     buf=input.get();

     while(!input.eof())

     {

         if(buf>='A'&&buf<='Z')

         {

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

            {

             if(buf==key[i])

             output.put(alpha[i]);

                       

         }                   

         else

         {

         cout<<”error in cipher text”;

         }

         output.put(buf);

         buf=input.get();

     }

     input.close();

     output.close();

     readText(inp);

     readText(out);

}

void cryptography::readText(char *inp)

{

     ifstream input;

   char buf;

     input.open(inp);

     cout<<" <--- "<<inp<<" ---> ";

     buf=input.get();

     while(!input.eof())

     {

         cout<<buf;

         buf=input.get();

     }

     input.close();

}

void main()

{

     cryptography a;

char alpha[26]= {‘a’,’b’,‘c’,’d’,‘e’,’f’,‘g’,’h’,‘i’,’j’,‘k’,’l’,‘m’,’n’,‘o’,’p’,‘q’,’r’,‘s’,’t’,‘u’,’v’ ,‘w’,’x’,‘y’,’z’};

     char key[26]= {‘t’,’s’,‘l’,’a’,‘u’,’e’,‘p’,’m’,‘f’,’q’,‘i’,’r’,‘z’,’c’,‘x’,’d’,‘o’,’g’,‘y’,’h’,‘v’,’b’ ,‘n’,’w’,‘j’,’k’};

     int choice;

     char inp[30],out[30];

     clrscr();

         cout<<" Enter input file: ";

         cin>>inp;

         cout<<" Enter output file: ";

         cin>>out;

        

     cout<<" 1. Encrypt 2. Decrypt Select choice(1 or 2): ";

     cin>>choice;

     if(choice==1)

         a.encrypt(inp,out,alpha,key);

     else if(choice==2)

         a.decrypt(inp,out,alpha,key);

     else cout<<" Unknown choice";

     getch();

}  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote