Question: The program takes a PPM image and will either encrypt or decrypt it when supplied with a key . Use the secret.ppm and key below

The program takes a PPM image and will either encrypt or decrypt it when supplied with a key. Use the secret.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

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

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

#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 h; rows++) {

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

for(; cols 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 h; rows++) {

for(cols = 0; cols 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 h; rows++) {

for(cols = 0; cols 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

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!