anar.bataa 0 Newbie Poster

Hi guys i have this code for multicast with encryption and decryption with DES(shared key) i think encryption goes ok but decryption not working and i can't find the problem both codes compiles fine without problem when i execute them encryption works but decryption not PLS HELP ME!!!... here is code. Receiver.c:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/des.h>

#define MAX_LEN  1024

char *Decrypt( char *Key, char *Msg, int size)
{

   static char*     Res;
   int              n=0;
   DES_cblock       Key2;
   DES_key_schedule schedule;

   Res = ( char * ) malloc( size );
   memcpy( Key2, Key,8);
   DES_set_odd_parity( &Key2 );
   DES_set_key_checked( &Key2, &schedule );

   DES_cfb64_encrypt( ( unsigned char * ) Msg, ( unsigned char * ) Res, size, &schedule, &Key2, &n, DES_DECRYPT );

return (Res);
}
int main(int argc, char *argv[])
{
    int     sock;
    int         flag_on = 1;
    struct      sockaddr_in multicast_addr;
    char        message_received[MAX_LEN+1];
    int             msgrecv_len;
    struct          ip_mreq mc_req;
    char*           multicast_ip;
    unsigned short  multicast_port;
    struct          sockaddr_in from_addr;
    unsigned int    from_len;
    char        *decrypted;
  FILE *fp;

  char key[512];
  fp=fopen("passphrase.txt", "r");
  if (fp) {
  while(fscanf(fp, "%s", key)!=EOF)
  fclose(fp);
}
printf("Key: %s\n", key);

if (argc != 3)
{
    fprintf(stderr, "Usage: %s Multicast_IP Multicast_Port\n", argv[0]);
    exit(1);
}
    multicast_ip = argv[1];          /* arg 1: multicast ip address */
    multicast_port = atoi(argv[2]);  /* arg 2: multicast port number */

if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
    perror("socket() failed");
    exit(1);
}
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
sizeof(flag_on))) < 0)
{
    perror("setsockopt() failed");
    exit(1);
}

memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family      = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
multicast_addr.sin_port        = htons(multicast_port);

if ((bind(sock, (struct sockaddr *) &multicast_addr, sizeof(multicast_addr))) < 0)
{
    perror("bind() failed");
    exit(1);
}

mc_req.imr_multiaddr.s_addr = inet_addr(multicast_ip);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
{
    perror("setsockopt() failed");
    exit(1);
}
    while(1)
{
    decrypted=malloc(sizeof(message_received));

    memcpy(decrypted,Decrypt(key,message_received,sizeof(message_received)), sizeof(message_received));

    memset(message_received, 0, sizeof(message_received));
    from_len = sizeof(from_addr);
    memset(&from_addr, 0, from_len);

if ((msgrecv_len = recvfrom(sock, message_received, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0)
{
    perror("recvfrom() failed");
    break;
}

printf("Received %d bytes from %s: ", msgrecv_len, inet_ntoa(from_addr.sin_addr));
printf("%s", decrypted);
}

if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
{
    perror("setsockopt() failed");
    exit(1);  
}
close(sock);
return 0;
}

and this is the result i'm getting:

bunny@ubuntu:~/Desktop/Client Receiver$ ./rec 224.0.1.14 6000

Key: qwerty123ABCD456

Received 3 bytes from 192.168.1.8: ��O�p1�z�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z�̚�w�z��^C

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.