Caesar Cipher简单程序

时间:2014-04-01 03:47:19

标签: c encryption

我从here获取了一个简单的Caesar密码的代码,我修改了它,以便用户定义密码密钥。但是每次我试图运行它时程序都会崩溃。

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

6 个答案:

答案 0 :(得分:5)

它并没有崩溃。 while循环立即结束,因为&#39; \ n&#39;在scanf之后的输入缓冲区中,这首先被读取

答案 1 :(得分:0)

读入密钥的scanf在输入缓冲区中留下换行符。然后,当您第一次致电getchar时,它会返回\n,因此永远不会输入while循环。

你实际上并没有崩溃,但是从来没有机会输入要编码的字符串。

您需要在getchar之后但在循环之前添加对scanf的额外调用以使用换行符:

scanf("%d",&z);
getchar();

答案 2 :(得分:0)

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

答案 3 :(得分:-2)

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

答案 4 :(得分:-2)

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

答案 5 :(得分:-2)

以下是C for Caesar密码的完整代码

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