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

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

ID: 3864246 • Letter: Q

Question

Q1: please help me with my C code to get the same this output without any random answer please:

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

my output:

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

The code:

include <stdio.h>

#include<string.h>

#include<stdlib.h>

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 < space + count1; i++)

pt[i] = '%';

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

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

int d = 0, j;

for (i = 0; i < add; i++)

for (j = 0; j < count; j++)

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

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

d = 0;

int k;

for (k = 0; k < count; k++)

{

if (key[k] >= '0' && key[k] <= '9')

j = key[k] - '0';

for (i = 0; i < add; i++)

{

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

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

}

}

entext[d--] = '';

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

d = 0;

for (k = 0; k < count; k++)

{

if (key[k] >= '0' && key[k] <= '9')

j = key[k] - '0';

for (i = 0; i < add; i++)

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

}

k = 0;

for (i = 0; i < add; i++)

for (j = 0; j < count; j++)

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

detext[count1 - 1] = '';

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

//

printf("************** <<<<<<<<< The Encryption >>>>>>>>> **************");

//

printf(" The Plaintext :");

for (i = 0; i < count1; i++)

//

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

//

printf("The Key : (");

for (k = 0; k < count; k++)

{

if (key[k] >= '0' && key[k] <= '9')

j = key[k] - '0';

//

printf("%d",j);

if (k != count - 1)

//

printf(",");

}

//

printf(")");

//

printf(" The Plaintext Matrix : ");

//

printf(" ");

for (i = 0; i < count; i++)

//

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

//

printf(" <==== The Key ");

for (i = 0; i < add; i++)

{

//

printf(" ");

for (j = 0; j < count; j++)

//

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

//

printf(" ");

}

printf("The Encrypted Text : ");

//

printf("%s",entext);

printf(" ************** <<<<<<<< The Decryption >>>>>>>>> ****************

");

//

printf(" The Ciphertext : ");

//

printf("%s",entext);

//

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

d = 0;

for (i = 0; i < count1 + space; i++)

{

{

if (d == add)

{

//

printf(" ");

d = 0;

}

//

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

d++;

}

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

for (k = 0; k < count; k++)

//

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

//

printf(" ");

for (i = 0; i < add; i++)

{

//

printf(" ");

for (j = 0; j < count; 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 Plaintext security security security The Key (4,1,2,3) The Plaintext Matrix 1 2 3 4 E The Key S e C r i t y s e C u r i t y s e c u r i The Encrypted Text uycte ixsr!uycteisr!uycteisr! KKKKKKKK The Decryption The Ciphertext uycteixsr!uycteisr!uycteisr The Ciphertext After Divided into Colums uycteix sr !uyct e isr !uy cteisr The Ciphertext Matrix With The Same Key 1 2 3 4 S e C r i t y s e c u r i t y s e c u r i t y The Decrypted Text :security!security!security

Explanation / Answer

Here's the code which is bug free. Do run the code. Save those 3 text files inside the directory where you have stored your C files. Hope it helps.

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *keyfile = NULL, *ptfile = NULL, *rfile = NULL;
int add, count = 0, count1 = 0,i,j,k,space, d=0;
char ch, pt[100], key[10], PTmat[10][10], entext[100], detext[100],Cmat[10][10];
system("clear");
//====================== 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;
space = add * count - count1;
for (i = count1; i < space + count1; i++)
pt[i] = '%';
pt[strlen(pt) + space] = '';
//========== GENERETE THE MATRIX =====
for (i = 0; i < add; i++)
for (j = 0; j < count; j++)
PTmat[i][j] = pt[d++];
//========== decryption ===============
d = 0;
for (k = 0; k < count; k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k] - '0';
for (i = 0; i < add; i++)
{
fputc(PTmat[i][j - 1], rfile);
entext[d++] = PTmat[i][j - 1];
}
}
entext[d--] = '';
//============= DECRYPTION =================
d = 0;
for (k = 0; k < count; k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k] - '0';
for (i = 0; i < add; i++)
Cmat[i][j - 1] = entext[d++];
}
k = 0;
for (i = 0; i < add; i++)
for (j = 0; j < count; j++)
detext[k++] = Cmat[i][j];
detext[count1 - 1] = '';
//================== PRINT THE RESULTS =====
//
printf("************** <<<<<<<<< The Encryption >>>>>>>>> **************");
//
printf(" The Plaintext :");
for (i = 0; i < count1; i++)
//
printf("%c",pt[i]);
//
printf("The Key : (");
for (k = 0; k < count; k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k] - '0';
//
printf("%d",j);
if (k != count - 1)
//
printf(",");
}
//
printf(")");
//
printf(" The Plaintext Matrix : ");
//
printf(" ");
for (i = 0; i < count; i++)
//
printf("%d ",i+1);
//
printf(" <==== The Key ");
for (i = 0; i < add; i++)
{
//
printf(" ");
for (j = 0; j < count; j++)
//
printf("%c ",PTmat[i][j]);
//
printf(" ");
}
printf("The Encrypted Text : ");
//
printf("%s",entext);
printf(" ************** <<<<<<<< The Decryption >>>>>>>>> ****************
");
//
printf(" The Ciphertext : ");
//
printf("%s",entext);
//
printf(" The Ciphertext After Divided into Colums: ");
d = 0;
for (i = 0; i < count1 + space; i++)
{
{
if (d == add)
{
//
printf(" ");
d = 0;
}
//
printf("%c",entext[i]);
d++;
}
printf(" The Ciphertext Matrix With The Same Key : ");
for (k = 0; k < count; k++)
//
printf("%d ",k+1);
//
printf(" ");
}
for (i = 0; i < add; i++)
{
//
printf(" ");
for (j = 0; j < count; j++)
//
printf("%c ",Cmat[i][j]);
//
printf(" ");
}
//
printf("The Decrypted Text :");   
printf("%s",detext);
getchar();
return 0;
}