ROOT
ROOT

凯撒密码

昨晚在知乎看到一个问题,( 可以帮忙破译一个密码吗?VRPHWLPHV L ZDQW WR FKDW ZLWK BRX

看了一下各位答案,是凯撒密码,我百科了一下,发现蛮简单的,今天早上来实现了。

凯撒加密方法就是将字符串的每个字母按字母表顺序进行偏移。

知道原理后,就可以很容易写出来了。

/*************************************************************************
    > File Name: caesar.c
    > Author: Netcan
    > Mail: 1469709759@qq.com 
    > Created Time: 2015/3/8 10:28:20
 ************************************************************************/
#include<stdio.h>
#include<string.h>
int main(int argc, char **argv)
{
	char code[1024];
	if(argc >= 3) {
		strcpy(code, argv[2]);
		for(char *c=code; *c!='\0'; ++c) {
			if(strcmp(argv[1], "-decode") == 0) 
				if(*c>='A' && *c<='Z') 
					*c = *c - 'A';
			if(strcmp(argv[1], "-code") == 0) 
				if(*c>='a' && *c<='z') 
					*c = *c - 'a';
		}
		for(int offset=1; offset<=26; ++offset) {
			for(char *c=code; *c!='\0'; ++c) 
				if(*c >= 0 && *c <= 25) {
					if(strcmp(argv[1], "-decode") == 0) 
						printf("%c", (*c + offset)%26 + 'a');
					if(strcmp(argv[1], "-code") == 0) 
						printf("%c", (*c + offset)%26 + 'A');
				}
				else
					printf("%c", *c);
			if(strcmp(argv[1], "-decode") == 0) 
				printf("offset=%d\n",offset);
			if(strcmp(argv[1], "-code") == 0) 
				printf("offset=%d\n",27-offset);
		}
	}
	return 0;
}

程序运行也很简单,

caesar.exe -code "string"  #加密
caesar.exe -decode "STRING" #解密

所以这个答案也很容易得出:

caesar.exe -decode "VRPHWLPHV L ZDQW WR FKDW ZLWK BRX, EXW L KDYH QR UHDVRQ WRFKDW ZLWK BRX."

解密的结果:

wsqixmqiw m aerx xs glex amxl csy, fyx m lezi rs viewsr xs glex amxl csy.       offset=1
xtrjynrjx n bfsy yt hmfy bnym dtz, gzy n mfaj st wjfxts yt hmfy bnym dtz.       offset=2
yuskzosky o cgtz zu ingz cozn eua, haz o ngbk tu xkgyut zu ingz cozn eua.       offset=3
zvtlaptlz p dhua av joha dpao fvb, iba p ohcl uv ylhzvu av joha dpao fvb.       offset=4
awumbquma q eivb bw kpib eqbp gwc, jcb q pidm vw zmiawv bw kpib eqbp gwc.       offset=5
bxvncrvnb r fjwc cx lqjc frcq hxd, kdc r qjen wx anjbxw cx lqjc frcq hxd.       offset=6
cywodswoc s gkxd dy mrkd gsdr iye, led s rkfo xy bokcyx dy mrkd gsdr iye.       offset=7
dzxpetxpd t hlye ez nsle htes jzf, mfe t slgp yz cpldzy ez nsle htes jzf.       offset=8
eayqfuyqe u imzf fa otmf iuft kag, ngf u tmhq za dqmeaz fa otmf iuft kag.       offset=9
fbzrgvzrf v jnag gb pung jvgu lbh, ohg v unir ab ernfba gb pung jvgu lbh.       offset=10
gcashwasg w kobh hc qvoh kwhv mci, pih w vojs bc fsogcb hc qvoh kwhv mci.       offset=11
hdbtixbth x lpci id rwpi lxiw ndj, qji x wpkt cd gtphdc id rwpi lxiw ndj.       offset=12
iecujycui y mqdj je sxqj myjx oek, rkj y xqlu de huqied je sxqj myjx oek.       offset=13
jfdvkzdvj z nrek kf tyrk nzky pfl, slk z yrmv ef ivrjfe kf tyrk nzky pfl.       offset=14
kgewlaewk a osfl lg uzsl oalz qgm, tml a zsnw fg jwskgf lg uzsl oalz qgm.       offset=15
lhfxmbfxl b ptgm mh vatm pbma rhn, unm b atox gh kxtlhg mh vatm pbma rhn.       offset=16
migyncgym c quhn ni wbun qcnb sio, von c bupy hi lyumih ni wbun qcnb sio.       offset=17
njhzodhzn d rvio oj xcvo rdoc tjp, wpo d cvqz ij mzvnji oj xcvo rdoc tjp.       offset=18
okiapeiao e swjp pk ydwp sepd ukq, xqp e dwra jk nawokj pk ydwp sepd ukq.       offset=19
pljbqfjbp f txkq ql zexq tfqe vlr, yrq f exsb kl obxplk ql zexq tfqe vlr.       offset=20
qmkcrgkcq g uylr rm afyr ugrf wms, zsr g fytc lm pcyqml rm afyr ugrf wms.       offset=21
rnldshldr h vzms sn bgzs vhsg xnt, ats h gzud mn qdzrnm sn bgzs vhsg xnt.       offset=22
sometimes i want to chat with you, but i have no reason to chat with you.       offset=23
tpnfujnft j xbou up dibu xjui zpv, cvu j ibwf op sfbtpo up dibu xjui zpv.       offset=24
uqogvkogu k ycpv vq ejcv ykvj aqw, dwv k jcxg pq tgcuqp vq ejcv ykvj aqw.       offset=25
vrphwlphv l zdqw wr fkdw zlwk brx, exw l kdyh qr uhdvrq wr fkdw zlwk brx.       offset=26

只有第 23 行的 "sometimes i want to chat with you, but i have no reason to chat with you.“有意义,所以,明文出来了,是 " 有时我想和你聊天,但是我没有理由和你聊天。

支持一下
扫一扫,支持Netcan
  • 微信扫一扫
  • 支付宝扫一扫