Question: Encoding messages! Caesar cipher PIC One of the most fundamental tasks in information processing is information encoding . It forms the bases of computer networks,

Encoding messages! Caesar cipher PIC

One of the most fundamental tasks in information processing is information encoding. It forms the bases of computer networks, data storage, compression (for images, audio, video, etc.), visualization, and security among others.

In this exercise, we will practice encoding information, our application will be one of the simplest forms of cryptography - the Caesar Cipher. The goal of the cipher is to encrypt an input text message in such a way that it looks like gibberish (it doesn't make sense). However, someone who knows how the message was encoded can easily retrieve the original text.

The Caesar cipher is a substitution cipher, each character is replaced by a different one by adding or subtracting a fixed value from it. For example, suppose we want to encode the text "Hello" using the Caesar cipher with a displacement of 2. The encrypted message is obtained as:

H + 2 = J

e + 2 = g

l + 2 = n

l + 2 = n

o + 2 = q

which gives "Jgnnq" - doesn't really look like our input does it? All we did is look for the letter 2 letters after the original and replace the original with that letter. The displacement is a parameter that can be changed, for example, we could choose a displacement of 4 , which gives the following encrypted text:

H + 4 = L

e + 4 = i

l + 4 = p

l + 4 = p

o + 4 = s

"Lipps"

The received can recover the original text if they have the encrypted version and know the displacement value used to encrypt the text, so in the last case, the received would take the encrypted text "Lipps" and obtain the original as:

L - 4 = H

i - 4 = e

p - 4 = l

p - 4 = l

s - 4 = o

"Hello"

This is a very simple cipher, but it demonstrates the process of encoding and decoding information. Let's implement it!

However - a simple cipher that uses a fixed displacement is rather easy to break - so, we thought of a nice way to make it crunchy (of course). Each letter will be encoded using a different displacement, we will get the displacements from an image by using the pixel values as displacement values for each of the letters in the message.

To exchange information, the sender and receiver would agree on an image only they have access to, the sender encodes the message using that image, then the receiver uses the same image on their end to recover the decoded message. Because the letters are encoded using different displacement values, it's much more challenging to crack the encryption than if we used a single displacement value.

Here's how the process works:

* The program will set up a message to be encoded

* The program will read a small (50x50 pixels) image from disk - this is a grayscale image, and as you know from Exercise 6 it's just stored as a 2D array of size 50x50, each value in the array is a number between 0 and 255.

* Now we go through the message using successive pixels in the image to obtain the displacement value used to encode that letter.

EXAMPLE WITH A VERY TINY IMAGE! AND MESSAGE (just so you see how it works, the actual message and image in the program are larger!)

Message: "Hello! I am a Message"

Image=[10 3 255 2

25 5 23 4

9 17 8 127

0 21 56 210]

The displacement values used for each letter are:

H+10

e+3

l+255

l+2

o+25

! + 5

+ 23

I + 4

+ 9

a + 17

m + 8

+ 127

a + 0

+ 21

M + 56

e + 210

s + 10

s + 3

a +255

g + 2

e + 25

Note : if we get to the end of the picture (bottom-right) and we still have message letters to encode, we just go right back to the beginning (top-left)

For this exercise you have two tasks:

- Implementing the Caesar cipher function to encode an input message using a specified input image.

* Your Caesar cipher function must respect the '\0' character. That means the 'end-of-string' delimiter must not be encoded, and no other character can become '\0'. If you look at an ASCII table and think about how this should work, you'll figure it out!

- Implementing a decoding function that takes a text encoded with the Caesar cipher, the image used to encode it, and returns the original text. Be smart about how you do this ;)

The starter code for this exercise is here:

caesar_cipher_pic.c Download caesar_cipher_pic.c

And here's a couple of images you can use for doing the encoding (you can use your own for testing, they have to be 50x50 in size, and .pgm in format)

tile_1.pgm Download tile_1.pgm

tile_2.pgm Download tile_2.pgm

Example output: **** Original input message: As much as you can, Even if you cannot shape your life as you want it, at least try this: as much as you can; do not debase it in excessive contact with the world, in the excessive movements and talk. Do not debase it by taking it, dragging it often and exposing it to the daily folly of relationships and associations, until it becomes burdensome as an alien life. Constantine P. Cavafy (1913) **** Encrypted text lines: ~yP||4~ZTSy?'k[q 3STOqz}&tgt:ZZ\M9hTz&/ YMNa'|-~|S6u]%N^{R}tNfM$[c:<\SzQ!_=mA{)S}YGJpUCdL@6(X_D?KVF :LRS/xqz?LV(FBRaPTh{STu_XMN_HsxvMcXTHX}OMZSYUDz~2QTRJaU~y=JOHM{uJWJSI~GYE\?tp **** Decoded text lines (should be identical to original input!): As much as you can, Even if you cannot shape your life as you want it, at least try this: as much as you can; do not debase it in excessive contact with the world, in the excessive movements and talk. Do not debase it by taking it, dragging it often and exposing it to the daily folly of relationships and associations, until it becomes burdensome as an alien life. Constantine P. Cavafy (1913)

THE ENCRYPTED MESSAGE PRINTED MAY LOOK DIFFERENT FOR YOU! - It depends on the specific keyboard and character encoding you're using in your computer, so you need to check the actual character values to make sure that your solution is correct, don't rely on the printed output.

//////////////////////////////////////////////////////////// // CSC A48 - Exercise 7 - Caesar Cipher Pic - Winter 2022 // // Do not add any additional functions // and do not leave any print statements // in your final submission for testing! // // (c) F. Estrada, Feb 2022. ///////////////////////////////////////////////////////////// #include #include #define MAX_STR_LEN 2048 void readPGMimage(const char *filename, unsigned char *image_array, int size_x, int size_y) { // You DO NOT have to read or understand this function! it is // needed here to enable us to import a small image to work // with for the exercise. // // Reads the image stored in the specified file into the // specified image array. The image is assumed to be of size // (size_x x size_y) pixels. This should match the array size! FILE *f; char line[1024]; int sizx,sizy; int i; f=fopen(filename,"rb+"); if (f==NULL) { fprintf(stderr,"Unable to open file %s for reading, please check name and path\ n",filename); return; } fgets(&line[0],1000,f); if (line[0]!='P'||line[1]!='5') { fprintf(stderr,"Wrong file format, not a .pgm file or header end-of-line characters missing "); fclose(f); return; } // Skip over comments fgets(&line[0],511,f); while (line[0]=='#') fgets(&line[0],511,f); sscanf(&line[0],"%d %d ",&sizx,&sizy); // Read file size if (sizx!=size_x||sizy!=size_y) { fprintf(stderr,"Image has different size from what was specified in the function call! "); fclose(f); return; }

fgets(&line[0],9,f); // Read the remaining header line fread(image_array,sizx*sizy*sizeof(unsigned char),1,f); fclose(f); return; } void caesar_cipher(unsigned char plaintext[MAX_STR_LEN], unsigned char passPic[50] [50]) { // This function encodes the input text in plaintext[] using the caesar // cipher process using the image in passPic[][] for displacement values. // See Exercise handout for details // // The function must ensure that the 'end-of-string' delimiter is preserved, // and that no other character can become the 'end-of-string'. // // You want to make sure you understand what values can be stored in each // entry of the plaintext[] string, and you'll want to look at an ASCII table // to check that your function does the right thing. // // *** TEST your code with multiple input strings, using different passwords, // and verify it works in // every case! if you get incomplete because your function failed our // tests it means you did not test carefully enough. } void caesar_decipher(unsigned char cryptotext[MAX_STR_LEN], unsigned char passPic[50][50]) { // This function reverses the encryption process and returns the original // text given the encrypted string and the passPic[][] // // The function has the same constraints as 'caesar_cipher()', that is, it // must respect the 'end-of-string' delimiter, and ensure no other // character becomes 'end-of-string' after decoding. // // Be smart about how you implement this function! // TO DO: Implement this function } #ifndef __testing int main() { unsigned char a_message[MAX_STR_LEN]="As much as you can, Even if you cannot shape your life as you want it, at least try this: as much as you can; do not debase it in excessive contact with the world, in the excessive movements and talk. Do not debase it by taking it, dragging it often and exposing it to the daily folly of relationships and associations, until it becomes burdensome as an alien life. Constantine P. Cavafy (1913) ";

unsigned char passPic[50][50]; readPGMimage("tile_1.pgm",&passPic[0][0],50,50); printf("**** Original input message: "); printf("%s",a_message); // Encode the input test lines caesar_cipher(a_message,passPic); printf(" **** Encrypted text lines: "); printf("%s",a_message); // Decode the encrypted strings caesar_decipher(a_message,passPic); printf(" **** Decoded text lines (should be identical to original input!):\ n "); printf("%s",a_message); return 0; } #endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!