Akshay Prabhakar
Akshay Prabhakar

Reputation: 479

Vigenère cipher, Issue in C

I am working on C program in vigenere ciphere in client and Server socket programming.

My Client End file :

#include<stdio.h>
#include<string.h>
#include<stdio.h> //printf
#include<string.h>    //strlen
#include<sys/socket.h>    //socket
#include<arpa/inet.h> //inet_addr

int main(int argc , char *argv[])
{
    int sock,i,j,msgLen,keyLen;
    struct sockaddr_in server;
    char message[1000] , server_reply[2000],ch,key[10],newKey[msgLen], encryptedMsg[msgLen];


    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    if (sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
        return 1;
    }

    puts("Connected\n");

    //keep communicating with server
    while(1)
    {       
        printf("Enter a message to encrypt :");
        scanf("%s",message);
        printf("enter key to be added :");
        scanf("%s",key);

        msgLen = strlen(message); 
        keyLen = strlen(key);

        //generating new key
        for(i = 0, j = 0; i < msgLen; ++i, ++j){
            if(j == keyLen)
                j = 0;

            newKey[i] = key[j];
        }

        newKey[i] = '\0';
        printf("\nNew Generated Key: %s", newKey);

        //encryption
        for(i = 0; i < msgLen; ++i)
            encryptedMsg[i] = ((message[i] + newKey[i]) % 26) + 'A';

        encryptedMsg[i] = '\0';


        printf("\nEncrypted Message: %s\n", encryptedMsg);


        //   puts("connected");    


        //Send some data
        if( send(sock , encryptedMsg , strlen(message) , 0) < 0)
        {
            puts("Send failed");
            return 1;
        }

    }     
    close(sock);
    return 0;
}

And Here is the Server File.

#include<stdio.h>
#include<string.h>    //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>    //write

int main(int argc , char *argv[])
{
    int socket_desc , client_sock , c , read_size,i,j,msgLen,keyLen;
    struct sockaddr_in server , client;
    char client_message[2000],key[10],ch,newKey[msgLen],decryptedMsg[msgLen];

    //Create socket
    socket_desc = socket(AF_INET , SOCK_STREAM , 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //Bind
    if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed. Error");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    //accept connection from an incoming client
    client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
    if (client_sock < 0)
    {
        perror("accept failed");
        return 1;
    }
    puts("Connection accepted");

    //Receive a message from client
    while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
    {
        printf("encrypted message from client :");
        puts(client_message);
        printf("enter key to be subtract :");
        scanf("%s",key);
        msgLen = strlen(client_message);keyLen = strlen(key);

        //generating new key
        for(i = 0, j = 0; i < msgLen; ++i, ++j){
            if(j == keyLen)
                j = 0;

            newKey[i] = key[j];
        }

        newKey[i] = '\0';
        printf("New Generated Key: %s", newKey);
        //decryption
        for(i = 0; i < msgLen; ++i)
           decryptedMsg[i] = (((client_message[i] - newKey[i]) + 26) % 26) + 'A';

        decryptedMsg[i] = '\0';

        printf("Decrypted Message: %s", decryptedMsg);
    }

    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    return 0;
}

When we send message from client end to server end it transfers the message to server end and when the key is entered to the server end nothing is happening.

Can anybody help ?

Upvotes: 0

Views: 125

Answers (1)

r3mainer
r3mainer

Reputation: 24547

Both your programs initialize the newKey[] array with an undefined length:

int msgLen;          // Value of msgLen is undefined
char newKey[msgLen]; // How big is newKey[]???

As always, this sort of problem could be identified by taking some simple steps:

  1. Get your compiler to report as many problems as possible

    Add -Wall (or better still, -Wall -Werror) to the command line arguments when compiling your code. This will inform you about potential errors in your code.

  2. Learn how to use a debugger

    Tools like gdb can often help you identify and fix problems much faster than posting code at Stack Overflow.

Upvotes: 4

Related Questions