zoukankan      html  css  js  c++  java
  • 凯撒密码加密C语言简单实现

    凯撒加密(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("
    ");
        }
        return 0;
    }

    然后就是这样子

    如输入

    Go head, make my day.

    3

    输出:Jr dkhdg, pdnh pb gdb.

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

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

    Jr dkhdg, pdnh pb gdb.

    23

    输出:Go head, make my day.

  • 相关阅读:
    Key and Certificate Conversion
    openssl
    python http通信实现
    鼠标右键添加cmd
    好文章
    wireshark里无网络接口解决办法
    python垃圾回收
    终于有人把 Docker 讲清楚了
    mongodb的监控与性能优化
    mongodb创建超级用户和普通用户(对应数据库的用户)
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/4008688.html
Copyright © 2011-2022 走看看