凯撒密码加密C语言简单实现

时间:2014-10-06 22:49:40   收藏:0   阅读:600

凯撒加密(Julius Caesar)该方法把一条消息中的每个字母用字母表中固定距离之后的那个字母代替。(如果超越了字母Z,会绕道字母表的起始位置。例如,如果每个字母都用字母表中两个位置之后的字母代替,那么Y就会被替换为A,Z就会被替换为B。)

然后编写程序…………

用户输入待加密的消息和移位数:

不是字母的不要改动…………

#include <stdio.h>
#include <string.h>

int main()
{
    char passwd[100],encrypted[100];
    int i,j,k,t,move;
    while(1)
    {
        printf("Enter message to be encrypted:");
        gets(passwd);
        printf("Enter shift amount(1-25):");
        scanf("%d%*c",&move);
        for(i=0; i<strlen(passwd); i++)
        {
            if(passwd[i] >= A && passwd[i] <= Z)
            {
                passwd[i] = ((passwd[i]-A)+move)%26+A;
            }
            else if(passwd[i] >= a && passwd[i] <= z)
            {
                passwd[i] = ((passwd[i]-a)+move)%26+a;
            }
        }
        printf("%s",passwd);
        printf("\n");
    }
    return 0;
}

然后就是这样子

如输入

Go head, make my day.

3

输出:Jr dkhdg, pdnh pb gdb.

………………………………………………

如果,输入这样就会解密:

Jr dkhdg, pdnh pb gdb.

23

输出:Go head, make my day.

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!