Vigenère密码,C中的问题

时间:2017-09-13 14:00:52

标签: c vigenere

我正在客户端和服务器套接字编程中使用vigenere ciphere中的C程序。

我的客户端文件:

#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;
}

这是服务器文件。

#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;
}

当我们从客户端向服务器端发送消息时,它会将消息传送到服务器端,当密钥输入服务器端时,没有任何事情发生。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

您的程序都会使用未定义的长度初始化newKey[]数组:

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

与往常一样,通过采取一些简单的步骤可以确定这类问题:

  1. 让编译器报告尽可能多的问题

    在编译代码时,将-Wall(或更好,-Wall -Werror)添加到命令行参数中。这将告知您代码中可能存在的错误。

  2. 了解如何使用调试器

    gdb这样的工具通常可以帮助您比在Stack Overflow上发布代码更快地识别和修复问题。