Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program takes a PPM image and will encrypt/decrypt when supplied with a key. This will use secret.ppm as the source, key as the crypto

This program takes a PPM image and will encrypt/decrypt when supplied with a key. This will use secret.ppm as the source, key as the crypto key, and decrypted.ppm as the location of the generated image.

Crypto.c contains segmentation faults and memory leaks. Fix these using GDB and Valgrind. Run decryption program to decrypt secrets.ppm.

Finally, decrypt the number hidden in the image. and then attempt to decrypt the number hidden in the image.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

// 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; void read_header(FILE*in_img, header_t*header); read_header(image_file,&header); read_image(image_file, &img, &header);

// Read secret key int seed; void read_seed(FILE*seed_file,int*seed); read_seed(key_file,&seed); 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; }

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// crypto.c //

#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); } /********************/ 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 h; row++) { image->data[row] = malloc(sizeof(pixel_t) * header->w * 3); for(; col w; col++) { pixel_t pix; fscanf(input, "%c%c%c", &pix.r, &pix.g, &pix.b); //FIXME: segfault here xxxxxxxxxxxxxx // image->data[row][col] = pix; } } } void read_seed(FILE* seed_file, int* seed) { fscanf(seed_file, "%d", seed); } 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! int row, col; switch (mode) { case 1: printf("Encrypt "); for(row = 0; row h; row++) { for(col = 0; col 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; //FIXME: segfault xxxxxxxxxxxxxxxxxxxxxx // 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 h; row++) { for(col = 0; col 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; } /********************/ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// crypto.h //

#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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Key: 22695477

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

secret.ppm

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions

Question

CL I P COL Astro- L(1-cas0) Lsing *A=2 L sin(0/2)

Answered: 1 week ago