凯撒密码


凯撒密码

凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

char st[1000];
void trlen(char st[],int len) {
    for(int i=0; i<len; i++) {
        st[i]=st[i]-'A'+'a';
    }
}
void miwen(char st[],int len) {
    for(int i=0; i<len; i++) {
        if(st[i]>='a'&&st[i]<='w') {
            st[i]+=3;
        } else if(st[i]>'w'&&st[i]<='z') {
            st[i]-=23;
        } else st[i]=st[i];
    }
    puts("密文:");
    puts(st);
}
void mingwen(char st[],int len) {
    for(int i=0; i<len; i++) {
        if(st[i]>='D'&&st[i]<='Z') {
            st[i]-=3;
        } else if(st[i]>='A'&&st[i]<'D') st[i]+=23;
        else st[i]=st[i];
    }
    trlen(st,len);
    puts("明文:");
    puts(st);
}
int main() {
    while(~scanf("%s",st)) {
        int len=strlen(st);
        mingwen(st,len);
        memset(st,0,sizeof(st));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值