Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started