凯撒密码
凯撒加密:是一种替换密码,通过将字母表中的每个字母移动固定数量的位置来加密信息。例如,若移动量为3,“A”会被替换为“D”,“B”替换为“E”,依此类推。在一些实现中,秘钥会自动根据随机数生成,并且每次加密时随机决定向前或向后移位,同时需记录加密时的随机数和移位方向作为秘钥用于解密。
import random
key = random.randint(0, 1000000000) % 26
direction = random.choice(["-", "+"])
key = eval(direction + str(key))
# print(direction)
print(key)
flag = "flag{y0u_ar3_g00d}"
def cry(s, key):
encode = ""
for i in s:
if i.isupper():
encode += chr((ord(i) - ord("A") + key) % 26 + ord("A"))
elif i.islower():
encode += chr((ord(i) - ord("a") + key) % 26 + ord("a"))
else:
encode += i
return encode
ss = cry(flag, key)
print(ss)
def nocry(s):
decode = ""
for i in range(26):
for j in s:
if j.isupper():
decode += chr((ord(j) - ord("A") - i) % 26 + ord("A"))
elif j.islower():
decode += chr((ord(j) - ord("a") - i) % 26 + ord("a"))
else:
decode += j
decode += "\n"
return decode
sss = nocry(ss)
print(sss)