下面是实验的代码
import sys
password=[]
dspassword=[]
def ciper(plain,value):
times=0
secret_list=list(plain)
secret_list_len=len(secret_list)
try:
value=int(value)
except ValueError:
print("Please input an integer.")
sys.exit()
print("")
print("Caesar cipher is : ",end='')
while times < secret_list_len:
times=times+1
ansi_raw=ord(secret_list[0+times-1])
ansi=ansi_raw+int(value)
word=(secret_list[0+times-1])
if (ansi_raw < 65 or ansi_raw > 90) and word.islower() == False :
print(word,end='')
elif (ansi_raw < 97 or ansi_raw > 122) and word.isupper() == False:
print(word,end='')
else:
while word.isupper() == True and ansi > 90:
ansi = -26 + ansi
while word.isupper() == True and ansi < 65:
ansi = 26 + ansi
while word.isupper() == False and ansi > 122:
ansi = -26 + ansi
while word.isupper() == False and ansi < 97:
ansi = 26 + ansi
print(chr(ansi),end='')
password.append(chr(ansi))
print("--------------")
return password
def kaisa_diciper(ciphertext):
ciphertext_list=list(ciphertext)
secret_list_len=len(ciphertext_list)
for value in range(26):
ds_pass=[]
times=0
try:
value=int(value)
except ValueError:
print("Please input an integer.")
sys.exit()
print("")
print("Decryption of Kaiser is : ",end='')
while times < secret_list_len:
times=times+1
ansi_raw=ord(ciphertext_list[0+times-1])
ansi=ansi_raw+int(value)
word=(ciphertext_list[0+times-1])
if (ansi_raw < 65 or ansi_raw > 90) and word.islower() == False :
print(word,end='')
elif (ansi_raw < 97 or ansi_raw > 122) and word.isupper() == False:
print(word,end='')
else:
while word.isupper() == True and ansi > 90:
ansi = -26 + ansi
while word.isupper() == True and ansi < 65:
ansi = 26 + ansi
while word.isupper() == False and ansi > 122:
ansi = -26 + ansi
while word.isupper() == False and ansi < 97:
ansi = 26 + ansi
print(chr(ansi),end='')
ds_pass.append(chr(ansi))
print("--------------")
dspassword.append(ds_pass)
return dspassword
def encrypt(encry_str):
str_a=[]
str_b=[]
encry_str=list(encry_str)
length=len(encry_str)
if (length%2==0):
for i in range(0,length,2):
print(i)
str_a.append(encry_str[i])
str_b.append(encry_str[i+1])
encry_str="".join(str_a+str_b)
print(encry_str)
else:
for i in range(0,length):
if(i%2==0):
str_a.append(encry_str[i])
else:
str_b.append(encry_str[i])
encry_str="".join(str_a+str_b)
print("加密之后为"+encry_str)
def decipher(dec_str):
dec_str_len=len(dec_str)
dec_str=list(dec_str)
field=[]
if ((dec_str_len%2)==0):
space_len=(dec_str_len//2)
for i in range(space_len):
field.append(dec_str[i])
field.append(dec_str[i+space_len])
else:
space_len=(dec_str_len//2)+1
for i in range(space_len):
field.append(dec_str[i])
if(i<(space_len-1)):
field.append(dec_str[i+space_len])
print("栅栏解密后为:")
print(field)
return field
ok="y"
while ok=="y":
choice=input("If you want to encrypt the string, please input 1\nIf you want to decrypt the string, please input anything\n")
if choice=="1":
plain=input("Please input your plain text: \n")
value=input("Please input your key(included negatives): \n")
encry_str=ciper(plain,value)
encrypt(encry_str)
else :
dec_str=input("Please input your ciphertext: \n")
ciphertext=decipher(dec_str)
kaisa_diciper(ciphertext)
ok=input("If you continue, please enter y\n\n")