[英]Caesar Cipher Simple Program
我從這里獲取了一個簡單的凱撒密碼的代碼,我已經修改了它,以便用戶定義密碼密鑰。 但是每次我嘗試運行它時程序都會崩潰。
#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;
}
它不會崩潰。 while 循環立即結束,因為 '\\n' 在 scanf 之后位於輸入緩沖區中,並且首先讀取
讀入鍵的scanf
在輸入緩沖區中留下一個換行符。 然后當您第一次調用getchar
時,它返回\\n
,因此永遠不會進入while
循環。
您實際上並沒有崩潰,只是從來沒有機會輸入要編碼的字符串。
您需要在scanf
但在使用換行符的循環之前添加對getchar
的額外調用:
scanf("%d",&z);
getchar();
#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);
}
}
#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();
}
#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;
}
這是凱撒密碼的 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]='\0';
printf("After ciphering: %s",cipher);
getch();
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.