我已经用c写了这个ceaser密码程序,但是每次我运行它时它都会崩溃

我已经用c写了这个ceaser密码程序,但是每次我运行它时它都会崩溃,c,encryption,caesar-cipher,C,Encryption,Caesar Cipher,我用c语言编写了这个caesar密码程序,在我提供密钥的整数值之前,它运行良好,但在这之后它崩溃了。 有人能纠正这个代码吗 #include <stdio.h> #include <stdlib.h> #include <string.h> char alphabets[] = "abcdefghijklmnopqrstuvwxyz"; int arrow,key; int search(char x) { for(arrow=0;arrow<s

我用c语言编写了这个caesar密码程序,在我提供密钥的整数值之前,它运行良好,但在这之后它崩溃了。 有人能纠正这个代码吗

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char alphabets[] = "abcdefghijklmnopqrstuvwxyz";
int arrow,key;
int search(char x)
{
    for(arrow=0;arrow<strlen(alphabets);arrow++)
    {

        if(x==alphabets[arrow])
        {
        return arrow;
        }
    }
}
char cipherGenerator(int arrow)
{
    arrow=arrow+key;
    if(arrow>strlen(alphabets))
    {
        arrow = (arrow%strlen(alphabets))-1;
    }
    return alphabets[arrow];
}

int main()
{
    char plain_text[]="",cipher_text[]="";
    int i;
    printf("Enter the plain text\n");
    gets(plain_text);
    printf("Enter the key\n");
    scanf("%d",&key);
    for(i=0;i<strlen(plain_text);i++)
    {
      strcat(cipher_text,cipherGenerator(search(plain_text[i])));
    }
    printf("The cipher text is %s:-",cipher_text);
    return 0;
}
#包括
#包括
#包括
字符字母[]=“abcdefghijklmnopqrstuvwxyz”;
int箭头键;
整数搜索(字符x)
{
用于(箭头=0;箭头字符串(字母表))
{
箭头=(箭头%strlen(字母表))-1;
}
返回字母[箭头];
}
int main()
{
字符纯文本[]=“”,密码文本[]=“”;
int i;
printf(“输入纯文本\n”);
获取(纯文本);
printf(“输入密钥”);
scanf(“%d”和键)(&key);

对于(i=0;i而言,崩溃可以通过尝试写入长度为1的数组来解释

char plain_text[]="",cipher_text[]=""; //these arrays have length 1

gets(plain_text); //will fail and crash here
strcat(cipher_text,cipherGenerator(search(plain_text[i]))); //will crash here
关于使用:

gets()函数不执行边界检查,因此此函数极易受到缓冲区溢出攻击。无法安全使用(除非程序在限制stdin上显示内容的环境中运行).因此,C99标准的第三个勘误表中不推荐使用该函数,并在C11标准中完全删除了该函数。建议使用fgets()和gets_s()

永远不要使用gets()。


plain_text
cipher_text
被分配为长度为1的数组。
get
不应被使用。可能还存在其他问题。存在许多问题。在启用所有警告的情况下进行编译并将其视为错误。您可能希望开始阅读C教科书。
strcat
用于添加字符串,而不是字符串单个字符。