Encrypt and Decrypt text

Encrypt and Decrypt Text

#include <sys/types.h>

#include <sys/ipc.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <sys/wait.h>

#define SIZE 100

////////////////////////////////////////////

void  encryptEVEN(const char *c ,char *EVEN){

inti,j;

i=0;

j=0;

while(c[i]!=’\0′){

if(i%2==0){ EVEN[j++]=c[i];}

i++;

}

EVEN[j]=’\0′;

}

/////////////////////////////////////////

void  encryptODD(const char *c ,char *ODD){

inti,j;

i=0;

j=0;

while(c[i]!=’\0′){

if(!(i%2==0)){ ODD[j++]=c[i];}

i++;

}

ODD[j]=’\0′;

}

////////////////////////////////////////

voiddecrept(char *EVEN , char *ODD){

char temp1[SIZE],temp2[SIZE];

int i;

i=0;

while((EVEN[i] !=’\0′ || ODD[i] !=’\0′) && i<sizeof(temp1) && i<sizeof(temp2)){

if(EVEN[i] !=’\0’||ODD[i] !=’\0′){

temp1[i]=EVEN[i];

temp2[i]=ODD[i];

printf(“%c%c”,temp1[i],temp2[i]);

i++;}

}

}

///////////////////////////////////

int main()

{

pid_tpid;

int j;

j=0;

charbuf[200];

int pfds1[2],pfds2[2],pfds3[2];

charcharr[SIZE], EVEN[SIZE], ODD[SIZE];

if (pipe(pfds1) ==-1 || pipe(pfds2) ==-1 || pipe(pfds3) ==-1)

{

perror(“pipe failed”);

exit(1);

}

pid=fork();

switch(pid){

case -1: printf(“fork failed\n”);exit(1);break;

/*child*/   case  0: sleep(1);

/*—————–recievemsg———–*/

close(pfds1[1]);// close write end

read(pfds1[0],EVEN,sizeof(EVEN));read(pfds1[0],ODD,sizeof(ODD));

printf(“The Decrypted Recieved massage is = “);

decrept(EVEN , ODD);

/*——————–replay————-*/

printf(“\nPlease Enter a replay : \n”);

while((j<strlen(charr)+1) && (charr[j]=getchar())!=’\n’)

{j++;}

if(strstr(charr,”bye”)||strstr(charr,”Bye”)){

printf(“The program is exiting !”);

exit(1); }

encryptEVEN(charr,EVEN);

encryptODD(charr,ODD);

//printf(“even= %sodd= %s \n” ,EVEN,ODD);

close(pfds2[0]);close(pfds3[0]);

write(pfds2[1],EVEN,sizeof(EVEN));

write(pfds3[1],ODD,sizeof(ODD));

sleep(1);

break;

/*parent*/  default: /*——————-send msg————-*/

while(1){

printf(“Enter the massage to be send :\n “);

while((j<strlen(charr)+1) && (charr[j]=getchar())!=’\n’)

{j++;}// get user input char by char

if(strstr(charr,”bye”)||strstr(charr,”Bye”)){

printf(“The program is exiting !”);

exit(1); }

encryptEVEN(charr,EVEN);

encryptODD(charr,ODD);

close(pfds1[0]);

write(pfds1[1],EVEN,sizeof(EVEN));

write(pfds1[1],ODD,sizeof(ODD));

sleep(1);

wait(NULL);

/*——————get replay————-*/

close(pfds2[1]);close(pfds3[1]);// close write end

while((read(pfds2[0],EVEN,sizeof(EVEN)))!=0 &&(read(pfds3[0],ODD,sizeof(ODD)))!=0){

//printf(“The recived massage is = “);

//printf(“%s    %s “,EVEN,ODD);

printf(“The Decrypted Recieved Replay is : “);

decrept(EVEN , ODD);//}}

}}}

exit(0);

return 0;

} 

Solution 

#include <sys/types.h>

#include <sys/ipc.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <sys/wait.h>

#define SIZE 100

////////////////////////////////////////////

voidencryptEVEN(const char *c, char *EVEN) {

int i, j;

i = 0;

j = 0;

while (c[i] != ‘\0’) {

if (i % 2 == 0) {

EVEN[j++] = c[i];

}

i++;

}

EVEN[j] = ‘\0’;

}

/////////////////////////////////////////

voidencryptODD(const char *c, char *ODD) {

int i, j;

i = 0;

j = 0;

while (c[i] != ‘\0’) {

if (!(i % 2 == 0)) {

ODD[j++] = c[i];

}

i++;

}

ODD[j] = ‘\0’;

}

////////////////////////////////////////

voiddecrept(char *EVEN, char *ODD, char* message) {

int i = 0, j = 0;

while (EVEN[i] != ‘\0’ || ODD[i] != ‘\0’) {

if(EVEN[i] != ‘\0’){

message[j] = EVEN[i];

j++;

}

if (ODD[i] != ‘\0’){

message[j] = ODD[i];

j++;

}

i++;

}

if(j >= 2*SIZE){

j = 2*SIZE -1;

}

message[j] = ‘\0’;

}

///////////////////////////////////

int main() {

pid_tpid;

int j;

j = 0;

int ch2par1[2], ch2par2[2], par2ch1[2], par2ch2[2];

charcharr[2 * SIZE], EVEN[SIZE], ODD[SIZE];

if (pipe(ch2par1) == -1 || pipe(ch2par2) == -1 || pipe(par2ch1) == -1

|| pipe(par2ch2) == -1) {

perror(“pipe failed”);

exit(1);

}

pid = fork();

if (pid == -1) {

printf(“fork failed\n”);

exit(1);

}

if (pid == 0) { /*child*/

close(ch2par1[0]);

close(ch2par2[0]);

close(par2ch1[1]);

close(par2ch2[1]);

sleep(1);

/*—————–recievemsg———–*/

while (1) {

read(par2ch1[0], EVEN, sizeof(EVEN));

read(par2ch2[0], ODD, sizeof(ODD));

memset(charr,0,sizeof(charr));

decrept(EVEN, ODD, charr);

printf(“The Decrypted Recieved massage is = %s\n”, charr);

//check charr message from the other proccess

intlast_message = 0; // bye  message flag

if (strstr(charr, “bye”) || strstr(charr, “Bye”)) {

last_message = 1;

}

/*——————–replay————-*/

else {

printf(“\nPlease Enter a replay : \n”);

memset(charr,0,sizeof(charr));

j = 0;

while ((j <strlen(charr) + 1) && (charr[j] = getchar()) != ‘\n’) {

j++;

}

}

 

if (strstr(charr, “bye”) || strstr(charr, “Bye”) || last_message) {

if (!last_message) {

encryptEVEN(“bye”, EVEN);

encryptODD(“bye”, ODD);

write(ch2par1[1], EVEN, sizeof(EVEN));

write(ch2par2[1], ODD, sizeof(ODD));

}

close(ch2par1[1]);

close(ch2par2[1]);

close(par2ch1[0]);

close(par2ch2[0]);

printf(“\n The child program is exiting !\n”);

exit(1);

}

encryptEVEN(charr, EVEN);

encryptODD(charr, ODD);

printf(“\n sending from child to parent : \n”);

write(ch2par1[1], EVEN, sizeof(EVEN));

write(ch2par2[1], ODD, sizeof(ODD));

sleep(1);

}

} else {

/*parent*//*——————-send msg————-*/

close(ch2par1[1]);

close(ch2par2[1]);

close(par2ch1[0]);

close(par2ch2[0]);

intlast_message = 0;

while (1) {

memset(charr,0,sizeof(charr));

if (!last_message) {

printf(“Enter the massage to be send :\n “);\

j = 0;

while ((j <strlen(charr) + 1) && (charr[j] = getchar()) != ‘\n’) {

j++;

}

}

if (strstr(charr, “bye”) || strstr(charr, “Bye”) || last_message) {

if (!last_message) {

encryptEVEN(“bye”, EVEN);

encryptODD(“bye”, ODD);

write(par2ch1[1], EVEN, sizeof(EVEN));

write(par2ch2[1], ODD, sizeof(ODD));

}

close(ch2par1[0]);

close(ch2par2[0]);

close(par2ch1[1]);

close(par2ch2[1]);

printf(“\n The parent program is exiting !\n”);

exit(1);

}

encryptEVEN(charr, EVEN);

encryptODD(charr, ODD);

write(par2ch1[1], EVEN, sizeof(EVEN));

write(par2ch2[1], ODD, sizeof(ODD));

sleep(1);

/*——————get replay————-*/

read(ch2par1[0], EVEN, sizeof(EVEN));

read(ch2par2[0], ODD, sizeof(ODD));

memset(charr,0,sizeof(charr));

decrept(EVEN, ODD, charr);

last_message = 0;

if (strstr(charr, “bye”) || strstr(charr, “Bye”)) {

last_message = 1;

} else {

printf(“The Decrypted Recieved Replay is : %s\n”, charr);

}

}

}

}