Beispiel #1
0
void decode()
{
	char line[1000];
	long int index;
	char decoding[] = {' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
						,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
						,'0','1','2','3','4','5','6','7','8','9'
						,',','.','!'};
	FILE *fpRdRcvd,*fpWrRcvd;

	fpRdRcvd = fopen("receivedTemp.txt","r");
	if(fpRdRcvd==NULL){printf("Error opening file\n");exit(0);}

	fpWrRcvd = fopen("ReceivedFile.txt","w");
	if(fpWrRcvd==NULL){printf("Error opening write file\n");exit(0);}

  if(fgets(line,sizeof(line),fpRdRcvd)==NULL)printf("Empty File\n");

  fseek(fpRdRcvd,0,SEEK_SET);
	while(fgets(line,sizeof(line),fpRdRcvd)!=NULL)
	{
		if(line[0]=='\n')fputs(line,fpWrRcvd);
		else
		{
			index = atoi(line);
			index = DecryptionAlgorithm(index,pvt_key);
			fprintf(fpWrRcvd,"%c",decoding[index]);
		}
		
	}
/*  fseeko(fpWrRcvd,-6,SEEK_END);
  off_t position = ftello(fpWrRcvd);
  ftruncate(fileno(fpWrRcvd), position);*/

fclose(fpRdRcvd);
fclose(fpWrRcvd);

}
/* Interaction with the server */
void Talk_to_server ( int cfd )
{
   int cont;
   int nbytes, status;
   int src_addr, dest_addr;
   char filename[25], id[10], pw[10];
   Msg send_msg;
   Msg recv_msg;
   long int q, alpha, secret_key, public_key, private_key;
   long int X_A, Y_A, Y_B;
   FILE *fp;
   dest_addr = inet_addr("DEFAULT_SERVER");
   src_addr = inet_addr("192.168.1.245");
   
   printf("\n\nRegistration Phase:\n\n");
   printf("\nDo you want to register?(y:1/n:0)\t");
   scanf("%d", &cont);

   while(cont)
   {
	printf("\nEnter the ID:\t");
	scanf("%s", id);
	printf("\nEnter the Password:\t");
	scanf("%s", pw);

	send_msg.hdr.opcode = REGISTER;
   	send_msg.hdr.src_addr = src_addr;
   	send_msg.hdr.dest_addr = dest_addr;
   	strcpy(send_msg.m.auth.id, id);
   	strcpy(send_msg.m.auth.pw, pw);

	/* send the user's response to the server */
   	status = send(cfd, &send_msg, sizeof(Msg), 0);
   	if (status == -1)
   	{
      		fprintf(stderr, "*** Server error: unable to send\n");
      		return;
   	}

	printf("\nContinue with registration?(y:1/n:0)\t");
	scanf("%d", &cont);
   }

   printf("\n\nLogin Phase:\n");
   printf("\nEnter the ID:\t");
   scanf("%s", id);
   printf("\nEnter the Password:\t");
   scanf("%s", pw);

   send_msg.hdr.opcode = LOGINREQ;
   send_msg.hdr.src_addr = src_addr;
   send_msg.hdr.dest_addr = dest_addr;
   strcpy(send_msg.m.auth.id, id);
   strcpy(send_msg.m.auth.pw, pw);
             
   /* send the user's response to the server */
   status = send(cfd, &send_msg, sizeof(Msg), 0);
   if (status == -1)
   {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
   }
               

   /* Wait for responses from the server (Bob, User B) */
   while ( 1 )
   {

   	/* receive messages from server */
   	nbytes = recv(cfd, &recv_msg, sizeof(Msg), 0);
   	if (nbytes == -1)
	{
      		fprintf(stderr, "*** Client error: unable to receive\n");
      	}
 
   	switch ( recv_msg.hdr.opcode )
	{

		case LOGINREP : /* Login Reply */
				printf("\n\nMessage:: LOGINREP received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				if( (strcmp(recv_msg.m.buf, "Wrong ID/Password!") == 0) )
				{
					exit(0) ;
				}
				else if( (strcmp(recv_msg.m.buf, "Authenticated!") == 0) )
				{
					send_msg.hdr.opcode = PUBKEY; 
                			send_msg.hdr.src_addr = src_addr;
                			send_msg.hdr.dest_addr = dest_addr;
					printf("\nKey Generaion has been started:\n\r");	
   					KeyGeneration(&q, &alpha, &private_key, &public_key);
   					printf("Global public elements are q = %ld, alpha = %ld\n\r", q, alpha);

   					printf("Public Key for Alice (User A) is Y_A: %ld\n\r", public_key);

   					printf("Private key for Alice (User A) is X_A: %ld\n\r", private_key);
   
   					X_A = private_key;
   					Y_A = public_key;
					/* send the public key to Bob */
					printf("Sending the public key to Bob (User B)\n");
   					send_msg.m.dhpubkey.q = q;  /* q */
   					send_msg.m.dhpubkey.g = alpha; /* alpha */
   					send_msg.m.dhpubkey.Y = Y_A;  /* value of public key */	

					status = send(cfd, &send_msg, sizeof(Msg), 0);
   					if (status == -1)
					{
      						fprintf(stderr, "*** Client error: unable to send\n");
      						return;
    					}
				}
				
				break;
    
   		case PUBKEY : 	/* Public key */
                		printf("\n\nMessage:: PUBKEY received from source (%d)\n", recv_msg.hdr.src_addr); 

		               /* Compute the secret shared key based on public key received from Bob (User B) */  
        		        Y_B = recv_msg.m.dhpubkey.Y;
               			printf("Received public values from Bob are q = %ld, alpha = %ld, Y_B = %ld\n\r", 
           	            	recv_msg.m.dhpubkey.q, recv_msg.m.dhpubkey.g, Y_B);
               			secret_key = ModPower(Y_B, X_A, q);
               			printf("Computed secret shared key by Alice (User A) is %ld\n\r", secret_key);

				printf("\n\nEnter the file to be retrieved:\t");
				scanf("%s", filename);

               			/* Send an acknowledgement to Bob that the secret key is computed */       
                		send_msg.hdr.opcode = REQSERV;
                		send_msg.hdr.src_addr = src_addr;
                		send_msg.hdr.dest_addr = dest_addr;
                		strcpy(send_msg.m.buf, filename);

				fp = fopen(filename, "w"); // append mode
				fclose(fp);

				status = send(cfd, &send_msg, sizeof(Msg), 0);
   				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
      					return;
    				}
               			
				break;      
 
   		case ENCMSG : 	/* Encrypted msg */
              			printf("\n\nMessage:: ENCMSG received from source (%d)\n", recv_msg.hdr.src_addr);
           		   	printf("\nCiphertext received:\n\n%s\n", recv_msg.m.buf); 
              			/* decrypt the ciphertext */
               			strcpy(buffer, recv_msg.m.buf);
               			DecryptionAlgorithm(buffer, secret_key);
               			printf("\nRecovered plaintext:\n\n%s\n", buffer);   

				char ch;

  				fp = fopen(filename, "a"); // append mode
 
  				if( fp == NULL )
   				{
      					perror("Error while opening the file.\n");
    				  	exit(EXIT_FAILURE);
   				}
 				
				fwrite(buffer, 1, strlen(buffer), fp);
								
				fclose(fp);
    				printf("Written to file.\n");
				
				break;

		case REQCOM :	/* Request Complete */
				printf("\n\nMessage:: REQCOM received from source (%d)\n", recv_msg.hdr.src_addr);
           		   	printf("\n%s\n", recv_msg.m.buf);
				printf("\nDisconnecting from server!\n");
				send_msg.hdr.opcode = DISCONNECT;
                		send_msg.hdr.src_addr = src_addr;
                		send_msg.hdr.dest_addr = dest_addr;

				strcpy(send_msg.m.buf, "Disconnect...");
				status = send(cfd, &send_msg, sizeof(Msg), 0);
   				if (status == -1)
				{
      					fprintf(stderr, "*** Client error: unable to send\n");
      					return;
    				}

				break;

		case DISCONNECT :/* Disconnect */
				printf("\n\nMessage:: DISCONNECT received from source (%d)\n", recv_msg.hdr.src_addr);
				printf("%s\n", recv_msg.m.buf);
				exit(0);

				break;

   	}
   
  }

}
/* Interaction with the server */
void Talk_to_server ( int cfd ,char* agrv_filename)
{
    char str[STACK_SIZE],str_concat[STACK_SIZE];
    int x, e;
    FILE *fptr1;
    key pub_key, pvt_key;
    //char ch;
   char buffer[MAX_LEN];
   int nbytes, status;
   int src_addr, dest_addr;
  long int plaintext, ciphertext, deciphertext;
  KeyGeneration(&pub_key, &pvt_key);
   ReqMsg send_msg;
   RepMsg recv_msg;
   //key_public public_key;
   // printf("\n Public Key of Alice is (n,e): (%ld , %ld)\n\r", pub_key.public_key.n, pub_key.public_key.e);
 printf("\n Private key of Alice is (n,d): (%ld , %ld)\n\r", pvt_key.private_key.n,
  pvt_key.private_key.d);
   dest_addr = inet_addr(DEFAULT_SERVER);
   src_addr = inet_addr("127.0.0.5");

    printf("Sending the public_key to the server\n");          
    Msg msg;
    msg.hdr.opcode=Pubkey;
    msg.hdr.src_addr = src_addr;
    msg.hdr.dest_addr = dest_addr;
    msg.AllMsg.pubkey.n=pub_key.public_key.n;
    msg.AllMsg.pubkey.e=pub_key.public_key.e;
      int t_block=66,n_block=0;
    while(t_block<pub_key.public_key.n)
    {
      t_block=66+t_block*100;
      ++n_block;
    }
  status = send(cfd, &msg, sizeof(Msg), 0);
   if (status == -1) {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
    }
   /* send the request message REQ to the server */
   printf("Sending the request message REQ to the server\n");          
   msg.hdr.opcode = REQ;
   msg.hdr.src_addr = src_addr;
   msg.hdr.dest_addr = dest_addr;
   strcpy(msg.AllMsg.req.filename, agrv_filename);
   //printf("here\n");
   printf("File name is:- %s\n",msg.AllMsg.req.filename);
  

   status = send(cfd, &msg, sizeof(Msg), 0);
   if (status == -1) {
      fprintf(stderr, "*** Server error: unable to send\n");
      return;
    }
    //exit(0);
  while (1) {
  /* receive greetings from server */
   nbytes = recv(cfd, &msg, sizeof(Msg), 0);
   if (nbytes == -1) {
      fprintf(stderr, "*** Client error: unable to receive\n");
      
   }
   switch ( msg.hdr.opcode ) {
    
   case REP : 
                sprintf(str,"%ld",DecryptionAlgorithm(msg.AllMsg.rep.chyp,pvt_key));

                if(strlen(str)%2==1)
                {
                  strcpy(str_concat,"0");
                  strcat(str_concat,str);
                  strcpy(str,str_concat);
                }
                if(strlen(str)/(2*n_block)!=1)
                {
                  strcpy(str_concat,"00");
                  strcat(str_concat,str);
                  strcpy(str,str_concat);
                }
           /*printf("\nsha1 is:"); 
            int loop_print;
               for (loop_print = 0; loop_print < SHA_DIGEST_LENGTH; ++loop_print)
               {
                 printf("%u",msg.sha1_send[loop_print] );
               }*/
                
                //printf("final %s\n",str);
                substitute_back(str);
//printf("sha1 for sub%s\n",string_sha1);
               unsigned char digest[SHA_DIGEST_LENGTH];
               memset(digest,'\0',SHA_DIGEST_LENGTH);
               /*int x;
               for (x= 0; x < strlen(string_sha1); ++x)
               {
                 printf("string[%d]%c",x,string_sha1[x] );
               }
               printf("\n");*/
               //printf("size : %d\nText : #%s#", strlen(string_sha1), string_sha1);
               SHA1(string_sha1,strlen(string_sha1),digest);
               int for_loop;
               /*  for (i=0; i < SHA_DIGEST_LENGTH; i++) {
                 sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
                 }*/

                for (for_loop = 0; for_loop < SHA_DIGEST_LENGTH; ++for_loop)
                 {
                   if(msg.sha1_send[for_loop]!=digest[for_loop])
                   {
                      msg.hdr.opcode=Disc;
                      msg.AllMsg.disconnect.status=1;
                      status = send(cfd, &msg, sizeof(Msg), 0);
                   if (status == -1) {
                      fprintf(stderr, "hi Server error: unable to send\n");
                      return;
                    }
                   }
                 }
                 memset(string_sha1,'\0',strlen(string_sha1));
                 //printf("\n");
               //printf("substitute after %s\n", string_substitute);
                //printf("plaintext : %ld\n",plain);
              /* Check the status of REP message */
             /* if (msg.AllMsg.r.status) 
                printf("Message REQ has received successfully by the server\n");
              else    
               printf("Message REQ has NOT received successfully by the server\n");*/
              break; 
  case REQCOM:
               if(msg.AllMsg.reqcom.status==1)
               {
                msg.hdr.opcode=Disc;
                msg.AllMsg.disconnect.status=1;
                fptr1 = fopen(agrv_filename,"w");
                if (fptr1 == NULL)
                      printf("Error in writing to the file\n");
                fprintf(fptr1, "%s", string_substitute);
                fclose(fptr1);
               }
      printf("file recived complted doing deciphertext opcode is Disconnect: %d\n", msg.hdr.opcode);

               status = send(cfd, &msg, sizeof(Msg), 0);
                   if (status == -1) {
                    printf("Disconnect client opcode: %d\n", msg.hdr.opcode);
                      exit(0);
                    }
              //exit(0);
               break;
                                      
  case Disc :
                  printf("Disconnect client opcode: %d\n", msg.hdr.opcode);
                  exit(0);
                  break;
   default: 
            //printf("default\n");
            printf("message received with opcode: %d\n", msg.hdr.opcode);
            exit(0);  
   }
 }
}