The following program takes a PPM image and will either encrypt or decrypt it wh
ID: 3751522 • Letter: T
Question
The following program takes a PPM image and will either encrypt or decrypt it when supplied with a key. Find the errors and fix them, then run the program to decrypt the image. 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.
Fix the errors and determine the decrypted image below using the key and 3 files in the C program.
/********************************************/
key: 22695477
secret.ppm
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#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;
read_header(image_file, header);
read_image(image_file, &img, &header);
// Read secret key
int seed;
read_seed(key_file, *seed);
sp_rand(eed);
/** NO ERROR HERE **/
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;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include "crypto.h"
int P_RAND_SEED = 1;
/** NO ERRORS HERE! **/
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);
}
/********************/
/** Could be errors... **/
void read_image(FILE* input, img_t* image, header_t* header) {
int row, col;
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++) {
pixel_t pix
fscanf(input, "%c%c%c", &pix.r, &pix.g, &pix.b);
image.data[row][col] = pix;
}
}
}
/** NO ERRORS HERE! **/
void read_seed(FILE* seed_file, int* seed) {
fscanf(seed_file, "%d", seed);
}
/** Could be errors... **/
void sym_crypt(FILE* output, header_t* header, img_t* image, int mode) {
// Write header of PPM to files
/** NO ERRORS HERE! **/
fprintf(output, "%s %d %d %d ", header->type, header->w, header->h, header->cs);
/*********************/
// Encrypt or Decrypt!
// Could be errors...
int row, col;
switch (mode) {
case 1:
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--) {
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;
}
/** NO ERRORS HERE! **/
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);
}
}
/********************/
}
/** NO ERRORS HERE! **/
void sp_rand(unsigned int seed) {
P_RAND_SEED = seed;
}
/********************/
/** NO ERRORS HERE! **/
unsigned int p_rand(unsigned int init) {
return (P_RAND_SEED*init) % INT_MAX;
}
/********************/
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#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);
// Dumb random number generator
void sp_rand(unsigned int seed);
unsigned int p_rand(unsigned int init);
#endif
Explanation / Answer
#include "crypto.h"
int randSEED_p = 1;
void read_header(FILE in_img, header_t headr) {
headr->type = malloc(sizeof(char)*3);
fscanf(in_img, "%s ", headr->type);
fscanf(in_img, "%d %d ", &headr->w, &headr->h);
fscanf(in_img, "%d ", &headr->cs);
printf("headr: %s %d %d %d ", headr->type, headr->w, headr->h, headr->cs);
}
void read_image(FILE in, img_t imgss, header_t* headr) {
int row, col;
imgss->data = malloc(sizeof(pixel_t) headr->h);
for(row = 0; row < headr->h; row++) {
imgss->data[row] = malloc(sizeof(pixel_t) headr->w 3);
for(; col < headr->w; col++) {
pixel_t pixl;
fscanf(in, "%c%c%c", &pixl.r, &pixl.g, &pixl.b);
imgss->data[row][col] = pixl;
}
}
}
void read_seed(FILE seedFIle, int seeds) {
fscanf(seedFIle, "%d", seeds);
}
void sym_crypt(FILE out, header_t headr, img_t* imgss, int mode) {
fprintf(out, "%s %d %d %d ", headr->type, headr->w, headr->h, headr->cs);
int row, col;
switch (mode) {
case 1:
printf("Encrypt " );
for(row = 0; row < headr->h; row++) {
for(col = 0; col < headr->w; col++) {
int row_swap = p_rand(row*row) % headr->h;
int col_swap = p_rand(col*col) % headr->w;
pixel_t swapp = imgss->data[row][col];
imgss->data[row][col] = imgss->data[row_swap][col_swap];
imgss->data[row_swap][col_swap] = swapp;
}
} break;
case 2:
printf("Decrypt ");
for(row = headr->h-1; row >= 0; row--) {
for(; col >= 0; col--) {
int row_swap = p_rand(row*row) % headr->h;
int col_swap = p_rand(col*col) % headr->w;
pixel_t swapp = imgss->data[row][col];
imgss->data[row][col] = imgss->data[row_swap][col_swap];
imgss->data[row_swap][col_swap] = swapp;
}
} break;
default: break;
}
for(row = 0; row < headr->h; row++) {
for(col = 0; col < headr->w; col++) {
pixel_t pixl = imgss->data[row][col];
fprintf(out, "%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;
}
-----------------------------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
#include "crypto.h"
int main(int argc, char const *argv[]) {
FILE* imgFile = fopen(argv[1], "r");
FILE* key_file = fopen(argv[2], "r");
FILE* out_img = fopen(argv[3], "w");
header_t headr;
img_t imgs;
read_header(imgFile, &headr);
read_image(imgFile, &imgs, &headr);
int seeds;
read_seed(key_file, &seeds);
sp_rand(seeds);
printf("Encrypt[1] or Decrypt[2]? ");
int choice;
while(true) {
scanf("%d",&choice);
if(choice == 1 || choice == 2) {
sym_crypt(out_img, &headr, &imgs, choice);
break;
} else {
printf("Please enter 1 or 2 ");
}
}
fclose(imgFile);
fclose(key_file);
fclose(out_img);
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 in_img, header_t headr);
void read_image(FILE in_img, img_t imgss, header_t* headr);
void read_seed(FILE seedFIle, int seeds);
void sym_crypt(FILE out_img, header_t headr, img_t* imgss, int mode);
void sp_rand(unsigned int seeds);
unsigned int p_rand(unsigned int init);
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.