Vigenère 密码,C 中的问题
Posted
技术标签:
【中文标题】Vigenère 密码,C 中的问题【英文标题】:Vigenère cipher, Issue in C 【发布时间】:2017-09-13 14:00:51 【问题描述】:我正在客户端和服务器套接字编程中使用 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】:您的两个程序都使用未定义的长度初始化 newKey[]
数组:
int msgLen; // Value of msgLen is undefined
char newKey[msgLen]; // How big is newKey[]???
与往常一样,可以通过一些简单的步骤来识别此类问题:
让您的编译器尽可能多地报告问题
在编译代码时将-Wall
(或者更好的是-Wall -Werror
)添加到命令行参数中。这将通知您代码中的潜在错误。
了解如何使用调试器
gdb 之类的工具通常可以比在 Stack Overflow 上发布代码更快地帮助您识别和解决问题。
【讨论】:
感谢您的回复,我已根据您的建议更新了代码。但我没有找到任何运气。你能告诉我还有什么需要做的吗? 哦,对了。您需要将\n
放在server.c
中printf()
语句的末尾。控制台输出是行缓冲的,因此在您发送换行符之前,控制台中不会出现任何内容。
好的,为了更好的帮助,请您评论我需要在哪一行执行此操作?以上是关于Vigenère 密码,C 中的问题的主要内容,如果未能解决你的问题,请参考以下文章