以下功能要求用Python实现:求完整的代码。
小明开发了一个班级信息管理系统,用户通过帐号和密码进行登陆,为了确保用户帐号的安全,他需要将用户密码进行加密再保存。请你根据要求编制程序,完成小明的需求。
1.用户密码由7位纯数字组成,将其加密后输出。加密方法是将该数每一位上的数字加3然后除以10取余,作为该位上的新数字,最后将第一位与第四位交换,组成加密后的新数。(10分)
2.为了进一步提高保密性,用户密码由7位数字或小写字母组成。加密方法是判断该密码每一位字符,如果是数字,则该数字加3然后除以10取余,作为该位上的新字符;如果是小写字母,则将小写字母按顺序向后推3位作为该位上的新字符(如a加密后为d),如果超出小写字母z,则返回小写字母a继续循环。(8分)
3.设计解密程序,输入题2生成的密文能正确进行解密输出。(10分)
4.设计的程序有容错功能,题1中如果输入的7个字符不是纯数字,则提示“输入非法”;题2中如果输入的7个字符有不是数字或小写字母,则提示“输入非法”。(5分)
5.程序界面友好,有相应提示功能。(2分)
def encry1(pwd:str):
if not pwd.isnumeric(): return False
new=''
for i in pwd:
new+=str((int(i)+3)%10)
return new[3]+new[1:3]+new[0]+new[4:]
def encry2(pwd:str):
new=''
for i in pwd:
if i.isnumeric():
new+=str((int(i)+3)%10)
elif i.islower():
temp = ord(i)+3
if temp>122:temp-=26
new += chr(temp)
else:
return False
return new
def decry(pwd:str):
new = ''
for i in pwd:
if i.isnumeric():
new += str((int(i)+10)-3)[-1]
elif i.islower():
temp = ord(i)-3
if temp<97:temp+=26
new += chr(temp)
else:
return False
return new
method={'1':["加密",encry1],'2':["加密",encry2],'3':["解密",decry]}
while True:
m = input("请输入操作:1-加密一/2-加密二/3-解密:")
if m in "123":
break
pwd=input(f"请输入需要{method[m][0]}的7位密码:")
while len(pwd)!=7:
pwd=input("请输入7位密码:")
if method[m][1](pwd):
print(f"{method[m][0]}后的密码为:"+method[m][1](pwd))
else:
print("输入非法")
from email import message
import tkinter as tk
import tkinter.messagebox as msgbox
import string
window = tk.Tk()
window.geometry('400x200')
window.title('密码加/解密')
font = ('楷体',14,'bold')
plaintextStr = tk.StringVar()
ciphertextStr = tk.StringVar()
def encode1():
global plaintextStr,ciphertextStr
nums = '0123456789'
plaintext = plaintextStr.get()
if len(plaintext)<7:
msgbox.showerror('提示','密码不足7位')
return
ciphertext = [p for p in plaintext]
for i in range(len(ciphertext)):
if ciphertext[i] in nums:
ciphertext[i] = str((int(ciphertext[i])+3)%10)
else:
msgbox.showerror('提示','输入非法')
return
c0 = ciphertext[0]
ciphertext[0] = ciphertext[3]
ciphertext[3] = c0
ciphertextStr.set(''.join(ciphertext))
def encode2():
global plaintextStr,ciphertextStr
nums = '0123456789'
codes = string.ascii_lowercase
plaintext = plaintextStr.get()
if len(plaintext)<7:
msgbox.showerror('提示','密码不足7位')
return
ciphertext = [p for p in plaintext]
for i in range(len(ciphertext)):
if ciphertext[i] in nums:
ciphertext[i] = str((int(ciphertext[i])+3)%10)
elif ciphertext[i] in codes:
index = codes.find(ciphertext[i])
if index+3>len(codes)-1:
index = index+3-len(codes)
else:
index +=3
ciphertext[i] = codes[index]
else:
msgbox.showerror('提示','输入非法')
return
ciphertextStr.set(''.join(ciphertext))
def decode():
global plaintextStr,ciphertextStr
nums = '0123456789'
codes = string.ascii_lowercase
ciphertext = ciphertextStr.get()
plaintext = [c for c in ciphertext]
for i in range(len(plaintext)):
if plaintext[i] in nums:
if int(plaintext[i])<3:
plaintext[i] = str(int(plaintext[i])+7)
else:
plaintext[i] = str(int(plaintext[i])-3)
elif plaintext[i] in codes:
index = codes.find(plaintext[i])
if index-3<0:
index = index+len(codes)-3
else:
index -= 3
plaintext[i] = codes[index]
else:
msgbox.showerror('提示','输入非法')
return
plaintextStr.set(''.join(plaintext))
tk.Label(window,text='加密前:',justify=tk.RIGHT,width=80,font=font).place(x=30,y=50,width=80,height=30)
tk.Label(window,text='加密后:',justify=tk.RIGHT,width=80,font=font).place(x=30,y=100,width=80,height=30)
tk.Entry(window,justify=tk.RIGHT,width=180, font=font, textvariable = plaintextStr).place(x=130,y=50,width=180,height=30)
tk.Entry(window,justify=tk.RIGHT,width=180, font=font, textvariable = ciphertextStr).place(x=130,y=100,width=180,height=30)
#tk.Button(window,text='加密',command=encode1,font=font).place(x=100,y=150,width=80,height=40) #第一题用这行
tk.Button(window,text='加密',command=encode2,font=font).place(x=100,y=150,width=80,height=40) #第二题用这行
tk.Button(window,text='解密',command=decode,font=font).place(x=200,y=150,width=80,height=40)
window.mainloop()