C语言 凯撒密码简单程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22776422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Caesar Cipher Simple Program
提问by user3483503
I've taken a code from herefor a simple Caesar cipher, and I've modified it so that the user will define the cipher key. But the program crashes every time I tried to run it.
我从这里获取了一个简单的凯撒密码的代码,我已经修改了它,以便用户定义密码密钥。但是每次我尝试运行它时程序都会崩溃。
#include <stdio.h>
int main()
{
char array[100], cipher[100];
int c=0, x=0, y=0;
int z;
printf("This Program will encrypt according to your needs\n");
printf("Enter the cipher key\n");
scanf("%d",&z);
printf("Enter the sentence");
while((c=getchar()) != '\n')
{
array[x++]=(char)c;
cipher[y++]=(char)(c+z);
}
array[x]=0;
cipher[y]=0;
printf("%s\n",cipher);
return 0;
}
回答by avmohan
It doesn't crash. The while loop ends instantly since the '\n' is in input buffer after scanf and this gets read first
它不会崩溃。while 循环立即结束,因为 '\n' 在 scanf 之后位于输入缓冲区中,并且首先读取
回答by dbush
The scanfthat reads in the key leaves a newline in the input buffer. Then when you call getcharfor the first time, it returns \n, so the whileloop is never entered.
的scanf是,在关键读树叶在输入缓冲器中的换行符。然后当您getchar第一次调用时,它返回\n,因此while永远不会进入循环。
You're not actually crashing, but just never getting an opportunity to enter in the string to encode.
您实际上并没有崩溃,只是从来没有机会输入要编码的字符串。
You need to add an extra call to getcharafter the scanfbut before the loop to consume the newline:
您需要在循环getchar之后scanf但之前添加一个额外的调用以使用换行符:
scanf("%d",&z);
getchar();
回答by hardik gohel
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char p[20];
int key,i,enc;
clrscr();
printf("Enter Plain text=");
gets(p);
printf("\n Enter Key=");
scanf("%d",&key);
for(i=0;i<strlen(p);i++)
{
p[i]=tolower(p[i]);
enc=((p[i]-97)+key)%26;
printf("%c",enc+97);
}
}
回答by Sagun Shrestha
Here is a complete code in C for Caesar cipher
这是凯撒密码的 C 语言完整代码
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int i,key;
char plain[100],cipher[100];
printf("Enter key:");
scanf("%d",&key);
key=key%26; // adjusting key
fflush(stdin);
printf("Enter text:");
gets(plain);
for(i=0;i<strlen(plain);i++)
{
if(isalpha(plain[i]))
{
if(islower(plain[i]))
cipher[i]=(plain[i]+key-'a')%26+'a';
else
cipher[i]=(plain[i]+key-'A')%26+'A';
}
else
cipher[i]=plain[i];
}
cipher[i]='#include <stdio.h>
#include <conio.h>
void main()
{
int i, c;
char str[100];
printf("Enter the Text Message : ");
gets(str);
for (i = 0; i < strlen(str); i++)
{
switch (str[i])
{
case 'x':
str[i] = 'a';
continue;
case 'y':
str[i] = 'b';
continue;
case 'z':
str[i] = 'c';
continue;
case 'X':
str[i] = 'A';
continue;
case 'Y':
str[i] = 'B';
continue;
case 'Z':
str[i] = 'C';
continue;
}
if (str[i] >= 'a' && str[i] < 'x')
str[i] = str[i] + 3;
else if (str[i] >= 'A' && str[i] < 'X')
str[i] = str[i] + 3;
}
printf("Message After Encryption : \n");
puts(str);
for (i = 0; i < strlen(str); i++)
{
switch (str[i])
{
case 'a':
str[i] = 'x';
continue;
case 'b':
str[i] = 'y';
continue;
case 'c':
str[i] = 'z';
continue;
case 'A':
str[i] = 'X';
continue;
case 'B':
str[i] = 'Y';
continue;
case 'C':
str[i] = 'Z';
continue;
}
if (str[i] >= 'd' && str[i] <= 'z')
str[i] = str[i] - 3;
else if (str[i] >= 'D' && str[i] < 'Z')
str[i] = str[i] - 3;
}
printf("Message After Decryption : \n");
puts(str);
getch();
}
';
printf("After ciphering: %s",cipher);
getch();
return 0;
}
回答by user4627311
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAXSIZE 1024
int main(int argc, char* argv[])
{
char in[MAXSIZE];
char en[MAXSIZE] = {0};
//Input any Sting to encrypt in upper case or lower case
//or also mixed-up in any case.
read(STDIN_FILENO, in, MAXSIZE);
encrypt(in, en);
printf("%s\n%s\n", in, en);
bzero(in, strlen(in));
decrypt(en, in);
printf("%s\n%s\n", en, in);
return 0;
}
int encrypt(char* input, char* cipher)
{
int i;
for(i = 0; i < strlen(input); i++)
{
if(input[i] >= 97 && input[i] <= 122)
{
cipher[i] = input[i]+23 <= 122 ? input[i] + 23 : input[i] - 3;
}else if(input[i] >= 65 && input[i] <= 90)
{
cipher[i] = input[i]+23 <= 90 ? input[i] + 23 : input[i] - 3;
}else
cipher[i] = input[i];
}
return 0;
}
int decrypt(char* cipher, char* output)
{
int i;
for(i = 0; i < strlen(cipher); i++)
{
if(cipher[i] >= 97 && cipher[i] <= 122)
{
output[i] = cipher[i]-23 >= 97 ? cipher[i] - 23 : cipher[i] + 3;
}else if(cipher[i] >= 65 && cipher[i] <= 90)
{
output[i] = cipher[i]-23 >= 65 ? cipher[i] - 23 : cipher[i] + 3;
}else
output[i] = cipher[i];
}
return 0;
}

