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

Assignment: Caesar Block Cypher Write a program which reads the standard input,

ID: 3819223 • Letter: A

Question

 Assignment: Caesar Block Cypher  Write a program which reads the standard input,  stores text until it encounters EOF, then encrypts the text using the Caesar Block Cipher.  SAMPLE RUN:  a.out this is sample text suitable for a simulation of a diplomatic mission or a spy's instructions ^D  OUTPUT:  tpiaoosychltsnmi'tieaioaosistbmftnioieluaionnsxeldcrssstfaimatnasotpisrwmurilspul   Explanation:  Julius Caesar is known for a inventing two "field ciphers": the Shift, and the Block Transposition.  This is the latter.  It works like this: you figure out how big a square two-dimensional array you need to hold the text, then you create and fill the block with text writing from left to right/top to bottom, then you print it out from top to bottom/left to right.  By now, you understand arrays, and you understand for() loops, and you understand nesting, so this should be a walk in the park for you.  The message is: I embarked upon a dream voyage, willing to risk my soul.   The program reads in and stores:  Iembarkeduponadreamvoyagewillingtoriskmysoul  This message takes 44 characters.  To keep things simple, we want to use "square" tables.  Consider the following squares:  1 x 1 =  1 2 x 2 =  4 3 x 3 =  9 4 x 4 = 16 5 x 5 = 25 6 x 6 = 36 7 x 7 = 49  The nearest perfect square containing 44 is 49, or 7 x 7, so we allocate and fill a 7 x 7 block:  iembark edupona dreamvo yagewil lingtor iskmyso ulxyzab  Now, we encypher the message by printing it from top to bottom:  iedyliuedraislmuegnkxbpaegmyaomwtyzrnviosakaolrob  To decrypt it, we do exactly the same process: write the cyphertext left to right/top to bottom then read it out top to bottom/left to right.  iedyliu edraisl muegnkx bpaegmy aomwtyz rnviosa kaolrob  (Now before everybody starts banging tin pans on the bars: this is a lot more than the military cipher it appears. Yes, it's an historical method for scrambling and descrambling military messages, but it's also something we use frequently in programming.  In fact, you will use it later this semester for something completely different.)  STEPS TO A SOLUTION:  So: read your message into a large buffer or a string object.    Either remove the spaces and punctuation or not (it's harder for the Enemy to read if you do).   Then count the chars in the message.    Pick the first perfect square greater than the message length, allocate an array of char that size.  Read the message into a square array of that size from left to right, top to bottom.  Write the message out top to bottom, left to right, and you've encyphered it.  Learning outcomes:  Well, clearly, this teaches you how to process arrays with for() loops, plus it gets you to Master level on knowing how arrays are saved in the computer and how to access their elements.  There's another benefit to this one, but I'm not going to tell you right now.  You'll see it in a later assignment having nothing to do with cryptography.  TEST:  If you wrote a correct program, then this should output exactly what is input,(minus any spaces or punctuation you removed):  $ cat file | caesarb | caesarb  GRADING:  a.out < filename  a.out | a.out  l | a.out  a.out < hugefile.txt  Use the hints above to test your code. 

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <ctype.h>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int main()
{
int i;
string buff;
cout << "enter the text to be encoded" << endl;
getline(cin,buff);
cout << "done reading in the text!" << endl;
cout << "buffer size is " << buff.length() << endl;
cout << "the string is " << endl << buff << endl;
  
for ( i = buff.length()-1 ; i >=0 ; i--)
{
if ( !isalnum ( buff[i] ) )
{
buff.erase( i,1 );
}
}

int static SIZE = buff.length();
cout << "the string length is " << SIZE << endl;

int squared = static_cast <int> ( ceil(sqrt( static_cast <double> ( SIZE ))));
cout << "size of board is " << squared << endl;

char ** board;
board = new char *[squared];   
for ( i = 0 ; i < squared ; i++ )
board[i] = new char[squared];

i = 0;
for ( int c = 0 ; c < squared ; c++ )
for ( int r = 0 ; r < squared ; r++ )
board[r][c] = toupper(buff[i++]);

  
for ( int r = 0 ; r < squared ; r++ ){
for ( int c = 0 ; c < squared ; c++ ){
cout << board[r][c];}
cout << endl;
}

for ( i = 0 ; i < squared ; ++i )
delete [] board[i] ;

delete [] board;
}

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