python凯撒密码流程图_凯撒密码解密脚本(python)

def casearDecrypt(ciphertext, source_char, destination_char):

offset = ord(destination_char) - ord(source_char)

chars = "abcdefghijklmnopqrstuvwxyz"

for char in ciphertext:

is_upper_flag = 0

if char.isupper():

char = char.lower()

is_upper_flag = 1

if char not in chars:

outputChar(is_upper_flag, char)

continue

tempchar_ascii = ord(char) + offset

tempchar =chr(tempchar_ascii)

if tempchar not in chars:

if offset < 0:

tempchar_ascii += len(chars)

else:

tempchar_ascii -= len(chars)

tempchar = chr(tempchar_ascii)

outputChar(is_upper_flag, tempchar)

def outputChar(is_upper_flag, char):

if is_upper_flag == 1:

print(char.upper(), end="")

else:

print(char, end="")

ciphertext = input("Please input ciphertext:\n")

des_char = input("Please input destination_char:\n")

sors_char = input("Please input source_char:\n")

casearDecrypt(ciphertext, sors_char, des_char)

上面的脚本只能用于已知一个密文字母和明文字母相对应的情况。如果想要枚举所有情况,就不可以了,所以又重新编写改进,加入了枚举所有偏移的选项。改进后的代码如下:

def casearDecrypt(ciphertext, source_char, destination_char, list_all):

if list_all == True:

for offset in range(1, 27):

convertChar(offset)

else:

offset = ord(destination_char) - ord(source_char)

convertChar(offset)

def convertChar(offset):

chars = "abcdefghijklmnopqrstuvwxyz"

for char in ciphertext:

is_upper_flag = 0

if char.isupper():

char = char.lower()

is_upper_flag = 1

if char not in chars:

outputChar(is_upper_flag, char)

continue

tempchar_ascii = ord(char) + offset

tempchar =chr(tempchar_ascii)

if tempchar not in chars:

if offset < 0:

tempchar_ascii += len(chars)

else:

tempchar_ascii -= len(chars)

tempchar = chr(tempchar_ascii)

outputChar(is_upper_flag, tempchar)

print("")

def outputChar(is_upper_flag, char):

if is_upper_flag == 1:

print(char.upper(), end="")

else:

print(char, end="")

ciphertext = input("Please input ciphertext:\n")

while True:

operation = input("List all results?y/n:")

if operation == 'y' or operation == 'Y':

casearDecrypt(ciphertext, '', '', True)

break

elif operation == 'n' or operation == 'N':

des_char = input("Please input destination_char:\n")

sors_char = input("Please input source_char:\n")

casearDecrypt(ciphertext, sors_char, des_char, False)

break

else:

print("Input error! Please input y/n:")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值