Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q1: please help me with my C code to get the same this output I do not know what is the wrong with my code

Q1: please help me with my C code to get the same this output

image text in transcribed

I do not know what is the wrong with my code to be the output like that please help me

image text in transcribed

------------------------------------------------------------------

The code:

include

#include

#include

int main()

{

system("clear");

FILE *keyfile = NULL, *ptfile = NULL, *rfile = NULL;

int add, count = 0, count1 = 0;

char ch, pt[100], key[10], PTmat[10][10], entext[100], detext[100],Cmat[10][1\

0];

//====================== OPEN FILES =============

//

keyfile = fopen("key.txt","r");

if(keyfile == NULL)

{

printf(" Unable to open key.txt file ");

return -1;

}

//

ptfile = fopen("plain.txt","r");

if(ptfile == NULL)

{

printf(" Unable to open plain.txt file ");

return -2;

}

//

rfile = fopen("encrypt.txt","a");

if(rfile == NULL)

{

printf(" Unable to open encrypt.txt file ");

return -3;

}

strcpy(pt, "");

strcpy(entext, "");

//======== READING THE KEY FROM THE FILE =======

while (feof(keyfile) == 0)

{

ch = fgetc(keyfile);

key[count] = ch;

count++;

}

count--;

//======== READING THE PLAIN TEXT FROM THE FILE ======

while (feof(ptfile) == 0)

{

ch = fgetc(ptfile);

pt[count1] = ch;

count1++;

}

count1--;

add = count1 / count + 1;

int space = add * count - count1, i;

for (i = count1; i

pt[i] = '%';

pt[strlen(pt) + space] = '\0';

//========== GENERETE THE MATRIX =====

int d = 0, j;

for (i = 0; i

for (j = 0; j

PTmat[i][j] = pt[d++];

//========== decryption ===============

d = 0;

int k;

for (k = 0; k

{

if (key[k] >= '0' && key[k]

j = key[k] - '0';

for (i = 0; i

{

fputc(PTmat[i][j - 1], rfile);

entext[d++] = PTmat[i][j - 1];

}

}

entext[d--] = '\0';

//============= DECRYPTION =================

d = 0;

for (k = 0; k

{

if (key[k] >= '0' && key[k]

j = key[k] - '0';

for (i = 0; i

Cmat[i][j - 1] = entext[d++];

}

k = 0;

for (i = 0; i

for (j = 0; j

detext[k++] = Cmat[i][j];

detext[count1 - 1] = '\0';

//================== PRINT THE RESULTS =====

//

printf("************** >>>>>>>> **************");

//

printf(" The Plaintext :");

for (i = 0; i

//

printf("%c",pt[i]);

//

printf("The Key : (");

for (k = 0; k

{

if (key[k] >= '0' && key[k]

j = key[k] - '0';

//

printf("%d",j);

if (k != count - 1)

//

printf(",");

}

//

printf(")");

//

printf(" The Plaintext Matrix : ");

//

printf(" ");

for (i = 0; i

//

printf("%d ",i+1);

//

printf("

for (i = 0; i

{

//

printf(" ");

for (j = 0; j

//

printf("%c ",PTmat[i][j]);

//

printf(" ");

}

printf("The Encrypted Text : ");

//

printf("%s",entext);

printf(" ************** >>>>>>>> ****************\

");

//

printf(" The Ciphertext : ");

//

printf("%s",entext);

//

printf(" The Ciphertext After Divided into Colums: ");

d = 0;

for (i = 0; i

{

{

if (d == add)

{

//

printf(" ");

d = 0;

}

//

printf("%c",entext[i]);

d++;

}

printf(" The Ciphertext Matrix With The Same Key : ");

for (k = 0; k

//

printf("%d ",k+1);

//

printf(" ");

for (i = 0; i

{

//

printf(" ");

for (j = 0; j

//

printf("%c ",Cmat[i][j]);

//

printf(" ");

}

//

printf("The Decrypted Text :");

printf("%s",detext);

getchar();

return 0;

}

---------------------------------------------------------------------

3 FILES:

1. plain.txt

security!security!security!%%%

2. encrypt.txt

rsiet%u!rsi%sietcyetcyu!cyu!r%

3. key.txt

54123

The Encryption The Plaintext curity! The Plaintext Matrix 1 2 3 4 5 The Key S e C u r ity s e c u r i t y s e cur it The Encrypted Text rs ietautrs izs ietcyetcyutcyu KKKKKKKK The Decryption The Cipher text rsietzutrs i2sietcyetcyu!cyu!r% The Ciphertext After Divided into Colums rsiet u!rsin sietcy etcyu! cyutr The Ciphertext Matrix With The Same Key 1 2 3 4 5 S e c u r ity s e c u r i t y s e c u r i t The Decrypted Text :security!security!security

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

DB2 9 For Linux UNIX And Windows Advanced Database Administration Certification Certification Study Guide

Authors: Roger E. Sanders, Dwaine R Snow

1st Edition

1583470808, 978-1583470800

More Books

Students also viewed these Databases questions