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

Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fix

ID: 3725494 • Letter: R

Question

Random monoalphabet cipher. The Caesar cipher, which shifts all letters by a fixed amount, is far too easy to crack. Here is a better idea. As the key, don't use numbers but words. Suppose the key word is FEATHER. Then first remove duplicate letters, yielding FEATHR, and append the other letters of the alphabet in reverse order: Now encrypt the letters as follows: A B C D E F G H Write a program that encrypts or decrypts a file using this cipher. For example, crypt -d -kFEATHER encrypt.txt output.txt decrypts a file using the keyword FEATHER. It is an error not to supply a keyword

Explanation / Answer

Answer

c++ programm

#include <iostream>

#include <fstream>

#include<iomanip>

using namespace std;

//encryption

void lEncrypt(string,istream&,ostream&);

//decryption

void lDecrypt(string,istream&,ostream&);

string ladjust(string a);

int main(int argc, char * argv[])

{

     ifstream in;

     ofstream out;

     string lkey;

     in.open(argv[3]);

     if(in.fail())           

     {

          cout<<"file error ";

          system("pause");

          return 1;

     }

     out.open(argv[4]);

     lkey=argv[2];

     if(lkey[0]!='-'&&lkey[1]!='k')

     {

          cout<<"missing lkey program aborted ";

          return 1;

     }

     lkey=ladjust(lkey);

     cout<<lkey<<endl;

     if(strcmp(argv[1],"-d")==0)

     lDecrypt(lkey,in,out);

     else

     lEncrypt(lkey,in,out);   

     in.close();

     out.close();

     return 0;

}

string ladjust(string lkey)

{

     int lcount[26]={0};

     char lc;

     int li;

     bool lfound;

     string lnewkey="";

     for(li=2;lkey[li]!='';li++)

     {

          lkey[li]=toupper(lkey[li]);

          lcount[lkey[li]-'A']++;

     }

     for(li=0;li<26;li++)

     cout<<(char)('A'+li)<<" "<<lcount[li]<<endl;

     for(li=2;lkey[li]!='';li++)

     if(lcount[lkey[li]-'A']!=0)

     {

          lnewkey=lnewkey+lkey[li];

          lcount[lkey[li]-'A']=0;

     }

     int lLen=lnewkey.length();

     for(lc='Z';lc>='A';lc--)

     {

          lfound=false;

          cout<<lnewkey<<" "<<lnewkey.length()<<endl;

          for(li=0;li<lLen;li++)

          if(lc==lkey[li])

          {

              lfound=true;

              cout<<lc<<" "<<li<<endl;

          }

          cout<<lc<<" "<<lfound<<endl;    

          if(!lfound)   

          lnewkey=lnewkey+lc;

     }

     cout<<lnewkey<<endl;   

     return lnewkey;

}

void lEncrypt(string lw,istream& in,ostream& out)

{

     string lbuffer;

     int li;

     getline(in,lbuffer);

     while(in)

     {

          for(li=0;lbuffer[li]!='';li++)

          if(isalpha(lbuffer[li]))

          {

              out<<lw[toupper(lbuffer[li])-'A'];

              cout<<toupper(lbuffer[li])-'A'<<" "<<lw[toupper(lbuffer[li])-'A']<<" "<<li<<" "<<lbuffer[li]<<endl;

          }

          else

          out<<lbuffer[li];

          out<<endl;

          getline(in,lbuffer);

     }

}

void lDecrypt(string lw,istream& in,ostream& out)

{

     string lbuffer;

     int li,lj;

     getline(in,lbuffer);

     while(in)

     {

          for(li=0;lbuffer[li]!='';li++)

          if(isalpha(lbuffer[li]))

          {

              for(lj=0;lj<lw.length();lj++)

              if(toupper(lbuffer[li])==lw[lj])

              out<<(char)('A'+lj);

          }

          else

          out<<lbuffer[li];

          out<<endl;

          getline(in,lbuffer);

     }

}