凯撒密码,C语言,求救!(凯撒加密C语言)

2023-03-05 4:10:17 密码用途 思思

#include stdio.h

#include string.h

int main()

{

int i = 0;

int len = 0;

char ch;

char buf[256] = {0};

char nor[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

char enc[26] = {'s','u','w','y','a','c','e','g','i','k','m','o','q','r','t','v','x','z','b','d','f','h','j','l','n','p'};

printf("Encode or Decode: ");

scanf("%c",ch);

printf("please input your string: ");

fflush(stdin);

gets(buf);

len = strlen(buf);

switch (ch)

{

case 'e':

case 'E':

for (i=0;ilen;i++)

{

buf[i] = enc[buf[i] - 'a'];

}

break;

case 'd':

case 'D':

for (i=0;ilen;i++)

{

buf[i] = nor[i];

}

break;

default:

printf("wrong input!\n");

}

printf("%s\n",buf);

return 0;

}

C语言的凯撒加密

/*

和楼上的相比,或许 看上去很烦

ch[i] +=5;

if (ch[i] 'Z')

{

ch[i] -= 26;

}

可以改成和 楼上的 方法

等价于 ch[i] = 'A' + (ch[i] - 'A' + 5) % 26;

*/

# include stdio.h

# include stdlib.h //用到了system(); 不写 ,可以用 getchar();

#define strwidth 117 //定义长度

int main(void)

{

char ch[strwidth];

int i ;

printf("请输入密码:");

gets(ch); //输入数据,用gets(); 保留了空格

for (i = 0; i strwidth ;i++ )

{

if (ch[i] = 'a' ch[i] = 'z' ) //判断是否小写字母

{

ch[i] +=5;

if (ch[i] 'z') //不解释,我想这样,理解可能会方便点吧

{

ch[i] -= 26;

}

}

else if ( ch[i] = 'A' ch[i] = 'Z') //判断是否大写字母

{

ch[i] +=5;

if (ch[i] 'Z')

{

ch[i] -= 26;

}

}

}

printf("加密后为:%s\n" , ch); //输出数据

system("pause");

return 0;

}

/*

或者 这样

*/

# include stdio.h

# include stdlib.h //用到了system(); 不写 ,可以用 getchar();

#define strwidth 117 //定义长度

int main(void)

{

char ch[strwidth];

int i ;

printf("请输入密码:");

gets(ch); //输入数据,用gets(); 保留了空格

for (i = 0; i strwidth ;i++ )

{

if (ch[i] = 'a' ch[i] = 'z' || ch[i] = 'A' ch[i] = 'Z' ) //判断是否是字母

{

ch[i] +=5;

if ( ch[i]'Z' ch[i] = 'Z' + 5 || ch[i] 'z' )

{

ch[i] -= 26;

}

}

}

printf("加密后为:%s\n" , ch); //输出数据

system("pause");

return 0;

}

用C语言编程恺撒密码加密解密程序

#include stdio.h

#define isletter( c )    ( ((c)='a'(c)='z') || ((c)='A'(c)='Z') )

void Enc( const char *str, char *out, int key )

{

    int i = 0; 

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] + key;

            if ( ! isletter( out[i])  )

                out[i] -= 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

void Denc( const char *str, char *out, int key )

{

    int i=0;

    while( str[i] )

    {

        if ( isletter( str[i] ) )

        {

            out[i] = str[i] - key;

            if ( ! isletter( out[i] ) )

                out[i] += 26;

        }

        else

            out[i] = str[i];

        i++;

    }

    out[i] = 0;

}

int main()

{

    char  out[100], out2[100];

    Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 );

    printf( "%s\n", out );

    Denc( out, out2, 3 );

    printf( "%s\n", out2 );

}