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

The program takes a PPM image and will either encrypt or decrypt it when supplie

ID: 3751284 • Letter: T

Question

The program takes a PPM image and will either encrypt or decrypt it when supplied with a key. Use thesecret.ppm and key below as the source, key as the crypto key, and decrypted.ppm as the location of the generated image.

Determine the decrypted image below using the key and 3 files in the C program.

/********************************************/

key: 22695477

secret.ppm

/*********************************************************************/

#include "crypto.h"

int P_RandSeed = 1;

void read_header(FILE* input_Image, header_t* header) {

header->type = malloc(sizeof(char)*3);

fscanf(input_Image, "%s ", header->type);

fscanf(input_Image, "%d %d ", &header->w, &header->h);

fscanf(input_Image, "%d ", &header->cs);

printf("Header: %s %d %d %d ", header->type, header->w, header->h, header->cs);

}

void read_image(FILE* inputs, img_t* images, header_t* header) {

int rows, cols;

images->data = malloc(sizeof(pixel_t*) * header->h);

for(rows = 0; rows < header->h; rows++) {

images->data[rows] = malloc(sizeof(pixel_t) * header->w * 3);

for(; cols < header->w; cols++) {

pixel_t pix;

fscanf(inputs, "%c%c%c", &pix.r, &pix.g, &pix.b);

images->data[rows][cols] = pix;

}

}

}

void read_seed(FILE* seedFIle, int* seeds) {

fscanf(seedFIle, "%d", seeds);

}

void sym_crypt(FILE* output, header_t* header, img_t* images, int modee) {

fprintf(output, "%s %d %d %d ", header->type, header->w, header->h, header->cs);

int rows, cols;

switch (modee) {

case 1:

printf("Encrypt " );

for(rows = 0; rows < header->h; rows++) {

for(cols = 0; cols < header->w; cols++) {

int swap_row = p_rand(rows*rows) % header->h;

int swap_col = p_rand(cols*cols) % header->w;

pixel_t swap = images->data[rows][cols];

images->data[rows][cols] = images->data[swap_row][swap_col];

images->data[swap_row][swap_col] = swap;

}

} break;

case 2:

printf("Decrypt ");

for(rows = header->h-1; rows >= 0; rows--) {

for(; cols >= 0; cols--) {

int swap_row = p_rand(rows*rows) % header->h;

int swap_col = p_rand(cols*cols) % header->w;

pixel_t swap = images->data[rows][cols];

images->data[rows][cols] = images->data[swap_row][swap_col];

images->data[swap_row][swap_col] = swap;

}

} break;

default: break;

}

for(rows = 0; rows < header->h; rows++) {

for(cols = 0; cols < header->w; cols++) {

pixel_t pixl = images->data[rows][cols];

fprintf(output, "%c%c%c", pixl.r, pixl.g, pixl.b);

}

}

}

void sp_rand(unsigned int seeds) {

P_RAND_SEED = seeds;

}

unsigned int p_rand(unsigned int init) {

return (P_RAND_SEED*init) % INT_MAX;

}

/****************************************************************************/

//lab4.c

#include "stdio.h"

#include "stdlib.h"

#include "crypto.h"

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

FILE* imageFIle = fopen(argv[1], "r");

FILE* keyFile = fopen(argv[2], "r");

FILE* outImage = fopen(argv[3], "w");

// Read images

header_t header;

img_t imgs;

// read_header(imageFIle, header);
read_header(imageFIle, &header);

read_image(imageFIle, &imgs, &header);

// Read secret key

int seeds;

read_seed(keyFile, &seeds);

sp_rand(seeds);

printf("Encrypt[1] or Decrypt[2]? ");

int choice;

while(true) {

scanf("%d",&choice);

if(choice == 1 || choice == 2) {

sym_crypt(outImage, &header, &imgs, choice);

break;

} else {

printf("Please enter 1 or 2 ");

}

}

fclose(imageFIle);

fclose(keyFile);

fclose(outImage);

return 0;

}

/******************************************************************************/

#ifndef CRYPTO_H

#define CRYPTO_H value

#include "stdio.h"

#include "stdlib.h"

#include "limits.h"

#include "stdbool.h"

extern int P_RAND_SEED;

typedef struct header_t {

char* type;

int w, h, cs;

} header_t;

typedef struct pixel_t {

unsigned char r,g,b;

} pixel_t;

typedef struct img_t {

pixel_t** data;

} img_t;

void read_header(FILE* input_Image, header_t* header);

void read_image(FILE* input_Image, img_t* images, header_t* header);

void read_seed(FILE* seedFIle, int* seeds);

void sym_crypt(FILE* out_img, header_t* header, img_t* images, int modee);

// random number generator //

void sp_rand(unsigned int seeds);

unsigned int p_rand(unsigned int init);

#endif

Explanation / Answer

I ALREADY ANSWERED THIS IF YOU HAVE ANY DOUBT IN THIS ANSWER PLEASE COMMENT BELOW

ANS;

First of all we correct errors out of each file. The major suggestion is to look into the Function declaration or prototypes existing in the parallel header file and follow/conform to so as to.

crypto.h

No errors here

#ifndef CRYPTO_H
#define CRYPTO_H value
#include "stdio.h"
#include "stdlib.h"
#include "limits.h"
#include "stdbool.h"

extern int P_RAND_SEED;

typedef struct header_t {
   char* type;
   int w, h, cs;
} header_t;

typedef struct pixel_t {
   unsigned char r,g,b;
} pixel_t;

typedef struct img_t {
   pixel_t** data;
} img_t;

void read_header(FILE* in_img, header_t* header);
void read_image(FILE* in_img, img_t* image, header_t* header);
void read_seed(FILE* seed_file, int* seed);
void sym_crypt(FILE* out_img, header_t* header, img_t* image, int mode);

// random number generator //
void sp_rand(unsigned int seed);
unsigned int p_rand(unsigned int init);

#endif

main.c

#include "stdio.h"
#include "stdlib.h"
#include "crypto.h"

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

   FILE* image_file = fopen(argv[1], "r");
   FILE* key_file = fopen(argv[2], "r");
   FILE* out_image = fopen(argv[3], "w");

   // Read image
   header_t header;
   img_t img;
  
  
   Error 1:
   read_header(image_file, header);
  
   According to read_header declaration, 2nd argument
   should be a pointer to header_t i.e. header_t *
  
   void read_header(FILE* in_img, header_t* header);
  
   But here value was being passed in place of address
   leading to incompatible type error
   Fix is to pass the address of variable
  
   read_header(image_file, &header);
   read_image(image_file, &img, &header);

   // Read secret key
   int seed;

  
   Error 2:
   read_seed(key_file, *seed);
  
   Prototype for read_seed
  
   void read_seed(FILE* seed_file, int* seed);
  
   'seed' variable is of int type and not a pointer type
   thus we see error here, this is fixed by passing address
   of seed variable.
  
   read_seed(key_file, &seed);

  
   Error 3:
   sp_rand(eed);
  
   This is just a simple typing (aka typo) error
   it should be 'seed' rather than 'eed'
  
   sp_rand(seed);
  
   printf("Encrypt[1] or Decrypt[2]? ");
  
   int choice;
   while(true) {
   scanf("%d",&choice);
   if(choice == 1 || choice == 2) {
       sym_crypt(out_image, &header, &img, choice);
       break;
   } else {
       printf("Please enter 1 or 2 ");
   }
   }

   /********************/
   fclose(image_file);
   fclose(key_file);
   fclose(out_image);
  
   return 0;
}

crypto.c

#include "crypto.h"

int P_RAND_SEED = 1;

void read_header(FILE* in_img, header_t* header) {

header->type = malloc(sizeof(char)*3);
fscanf(in_img, "%s ", header->type);
fscanf(in_img, "%d %d ", &header->w, &header->h);
fscanf(in_img, "%d ", &header->cs);
printf("Header: %s %d %d %d ", header->type, header->w, header->h, header->cs);
}


Fix errors...
void read_image(FILE* input, img_t* image, header_t* header) {
int row, col;

Error 1:
image->data = malloc(sizeof(pixel_t*) * header.h);

As here header variable is a pointer to structure type
then right way/operator to get the value of h will be
arrow '->' operator

image->data = malloc(sizeof(pixel_t*) * header->h);
for(row = 0; row < header->h; row++) {
image->data[row] = malloc(sizeof(pixel_t) * header->w * 3);
for(; col < header->w; col++) {

Error 2:
pixel_t pix

';' is missing after the declaration

pixel_t pix;
fscanf(input, "%c%c%c", &pix.r, &pix.g, &pix.b);

Error 3:
image.data[row][col] = pix;

image variable is a pointer to structure type
then right way/operator to get the value of h will be
arrow '->' operator

image->data[row][col] = pix;
}
}
}

void read_seed(FILE* seed_file, int* seed) {
fscanf(seed_file, "%d", seed);
}

Fix errors...
void sym_crypt(FILE* output, header_t* header, img_t* image, int mode) {
// Write header of PPM to files
fprintf(output, "%s %d %d %d ", header->type, header->w, header->h, header->cs);


// Encrypt or Decrypt!
// fix errors //
int row, col;
switch (mode) {
case 1:

Error 4:

printf("Encrypt ", );

This seems to be typo error
As here printf is expecting some expression after ','

Now, as we don't have any format specifiers, we don't
need this ','

printf("Encrypt ");
for(row = 0; row < header->h; row++) {
for(col = 0; col < header->w; col++) {
int swap_row = p_rand(row*row) % header->h;
int swap_col = p_rand(col*col) % header->w;
pixel_t swap = image->data[row][col];
image->data[row][col] = image->data[swap_row][swap_col];
image->data[swap_row][swap_col] = swap;
}
} break;
case 2:
printf("Decrypt ");
for(row = header->h-1; row >= 0; row--) {
for(; col >= 0; col--) {

Error 5:

int swap_row = p_rand(row*row) % header.h;

As here header variable is a pointer to structure type
then right way/operator to get the value of h will be
arrow '->' operator

int swap_row = p_rand(row*row) % header->h;
int swap_col = p_rand(col*col) % header->w;
pixel_t swap = image->data[row][col];
image->data[row][col] = image->data[swap_row][swap_col];
image->data[swap_row][swap_col] = swap;
}
} break;
default: break;
}
for(row = 0; row < header->h; row++) {
for(col = 0; col < header->w; col++) {
pixel_t pix = image->data[row][col];
fprintf(output, "%c%c%c", pix.r, pix.g, pix.b);
}
}

}
void sp_rand(unsigned int seed) {
P_RAND_SEED = seed;
}

unsigned int p_rand(unsigned int init) {
return (P_RAND_SEED*init) % INT_MAX;
}

THANK YOU

PLZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ RATE THUMBS UP PLZZZZZZZZZZZZZZZ