凯撒密码
2016-07-17 本文已影响121人
michaeljacc
- 知乎有一个求助题, 破译密码的
- 当然了, 根据普通人定律, 小孩子喜欢用这种方式表白...
- 链接在此
https://www.zhihu.com/question/28324597
- 另, 这一看就是凯撒加密...
- 什么是凯撒密码:
凯撒密码作为一种最为古老的对称加密体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。
'VRPHWLPHV L ZDQW WR FKDW ZLWK BRX,EXW L KDYH QR UHDVRQ WR FKDW ZLWK BRX'
def lowercase(s):
result = ''
for c in s:
i = upper.find(c)
if i == -1:
# 没找到, 说明不是大写字母, 不做处理
result += c
else:
# 找到, 说明它是大写字母
result += lower[i]
return result
def decode(s, shift):
'''
:type s: str
:type shift: int
:rtype: str
:param s: 要解密的字符串
:param shift: 移动的位数
:return: 解密后的字符串
'''
result = ''
for c in s:
i = lower.find(c)
if i == -1:
result += c
else:
# 下面这个运算是为了保证下标不为负数, 反正要对 26 取模(求余数)
new_index = (i + 26 - shift) % 26
code = lower[new_index]
result += code
return result
s_lower = lowercase(s)
for i in range(26):
source = decode(s_lower, i)
print(source)
运行结果有26个,一目了然,藏在里面
- vrphwlphv l zdqw wr fkdw zlwk brx,exw l kdyh qr uhdvrq wr fkdw zlwk brx
- uqogvkogu k ycpv vq ejcv ykvj aqw,dwv k jcxg pq tgcuqp vq ejcv ykvj aqw
- tpnfujnft j xbou up dibu xjui zpv,cvu j ibwf op sfbtpo up dibu xjui zpv
- sometimes i want to chat with you,but i have no reason to chat with you
- rnldshldr h vzms sn bgzs vhsg xnt,ats h gzud mn qdzrnm sn bgzs vhsg xnt
- qmkcrgkcq g uylr rm afyr ugrf wms,zsr g fytc lm pcyqml rm afyr ugrf wms
- pljbqfjbp f txkq ql zexq tfqe vlr,yrq f exsb kl obxplk ql zexq tfqe vlr
- okiapeiao e swjp pk ydwp sepd ukq,xqp e dwra jk nawokj pk ydwp sepd ukq
- njhzodhzn d rvio oj xcvo rdoc tjp,wpo d cvqz ij mzvnji oj xcvo rdoc tjp
- migyncgym c quhn ni wbun qcnb sio,von c bupy hi lyumih ni wbun qcnb sio
- lhfxmbfxl b ptgm mh vatm pbma rhn,unm b atox gh kxtlhg mh vatm pbma rhn
- kgewlaewk a osfl lg uzsl oalz qgm,tml a zsnw fg jwskgf lg uzsl oalz qgm
- jfdvkzdvj z nrek kf tyrk nzky pfl,slk z yrmv ef ivrjfe kf tyrk nzky pfl
- iecujycui y mqdj je sxqj myjx oek,rkj y xqlu de huqied je sxqj myjx oek
- hdbtixbth x lpci id rwpi lxiw ndj,qji x wpkt cd gtphdc id rwpi lxiw ndj
- gcashwasg w kobh hc qvoh kwhv mci,pih w vojs bc fsogcb hc qvoh kwhv mci
- fbzrgvzrf v jnag gb pung jvgu lbh,ohg v unir ab ernfba gb pung jvgu lbh
- eayqfuyqe u imzf fa otmf iuft kag,ngf u tmhq za dqmeaz fa otmf iuft kag
- dzxpetxpd t hlye ez nsle htes jzf,mfe t slgp yz cpldzy ez nsle htes jzf
- cywodswoc s gkxd dy mrkd gsdr iye,led s rkfo xy bokcyx dy mrkd gsdr iye
- bxvncrvnb r fjwc cx lqjc frcq hxd,kdc r qjen wx anjbxw cx lqjc frcq hxd
- awumbquma q eivb bw kpib eqbp gwc,jcb q pidm vw zmiawv bw kpib eqbp gwc
- zvtlaptlz p dhua av joha dpao fvb,iba p ohcl uv ylhzvu av joha dpao fvb
- yuskzosky o cgtz zu ingz cozn eua,haz o ngbk tu xkgyut zu ingz cozn eua
- xtrjynrjx n bfsy yt hmfy bnym dtz,gzy n mfaj st wjfxts yt hmfy bnym dtz
- wsqixmqiw m aerx xs glex amxl csy,fyx m lezi rs viewsr xs glex amxl csy
知乎链接中的各个答案, 很多大神