import random
class bsttra:
'''“翻译器”类'''
def __init__(self,keys="嗷呜啊~",password=0x0123):
self.keys=keys
self.password=password
self.ans="暂无结果"
#self.zq=autoreturn
self.zhuanhua()
#self.dopping=dopping
def zhuanhua(self):
'''内部命令,将密码和字母表转化,在外部修改密码和字母表后请执行此函数'''
self.keylist=[self.keys[0],self.keys[1],self.keys[2],self.keys[3]]
self.passlist=[self.password//4096,(self.password//256)%16,(self.password//16)%16,self.password%16]
def reset(self):
'''重置参数为网络默认值'''
self.password=0x0123
self.keys="嗷呜啊~"
self.zhuanhua()
self.dopping=0
self.autoreturn=0
def encode(self,a):
'''加密'''
a=a.encode("utf-16")
ans=""
count=0
ans=ans+self.keys[3]+self.keys[1]+self.keys[0]
for i in range(0,len(a),2):
x=a[i]+(a[i+1]*256)
x1=((x//4096)+self.passlist[0])%16
x2=((x//256)%16+self.passlist[1])%16
x3=((x//16)%16+self.passlist[2])%16
x4=((x%16)+self.passlist[3])%16
if(count==4):
count=0
y1=(x1//4+count)%4
y2=x1%4
y3=(x2//4+count)%4
y4=x2%4
y5=(x3//4+count)%4
y6=x3%4
y7=(x4//4+count)%4
y8=x4%4
ans=ans+self.keys[y1]+self.keys[y2]+self.keys[y3]+self.keys[y4]+self.keys[y5]+self.keys[y6]+self.keys[y7]+self.keys[y8]
count+=1
ans+=self.keys[2]
self.ans=ans
return ans
def decode(self,a):
'''解密'''
counter=0
innercounter=0
x=0
x1=[0,0,0,0]
x2=[0,0,0,0,0,0,0,0]
ans=""
ans=ans.encode("utf-16")
starting=1
#解密过程
for i in range(len(a)):
if(a[i] in self.keylist):
if(a[i]==self.keylist[0]):
x2[innercounter]=0
elif(a[i]==self.keylist[1]):
x2[innercounter]=1
elif(a[i]==self.keylist[2]):
x2[innercounter]=2
else:
x2[innercounter]=3
innercounter+=1
if(starting==1):
if(innercounter==3):
innercounter=0
starting=0
if(innercounter==8):
innercounter=0
x2[0]=(x2[0]+4-counter)%4
x2[2]=(x2[2]+4-counter)%4
x2[4]=(x2[4]+4-counter)%4
x2[6]=(x2[6]+4-counter)%4
counter+=1
if(counter==4):
counter=0
x1[0]=x2[0]*4+x2[1]
x1[1]=x2[2]*4+x2[3]
x1[2]=x2[4]*4+x2[5]
x1[3]=x2[6]*4+x2[7]
x1[0]=(x1[0]+16-self.passlist[0])%16
x1[1]=(x1[1]+16-self.passlist[1])%16
x1[2]=(x1[2]+16-self.passlist[2])%16
x1[3]=(x1[3]+16-self.passlist[3])%16
x=(x1[0]*4096)+(x1[1]*256)+(x1[2]*16)+x1[3]
#print(x)
ans=ans+bytes([x%256,x//256])
ans=ans.decode("utf-16")
self.ans=ans
return ans
mode=0
#0=zcd,1=enc,2=dec,3=set,4=help,5=exit,6=about,7=showans
fyq=bsttra()
kpgo=0
#主循环
while(1):
#主菜单
if(mode==0):
commandin=input("MAINMENU>>>")
if(commandin==""):
mode=0
elif(commandin=="exitit"):
mode=5
elif(commandin=="help"):
mode=4
elif(commandin=="ans"):
mode=7
elif(commandin=="set"):
mode=3
elif(commandin=="encode"):
mode=1
elif(commandin=="decode"):
mode=2
elif(commandin=="about"):
mode=6
else:
print("""语法错误,键入"help"显示帮助""")
#加密
if(mode==1):
#输入
wordin=input("请输入附加命令")
if(wordin=="exitit"):
mode=0
continue
elif(wordin=="setkey"):
keysin=input("请输入使用的字符")
if(len(keysin)==4):
fyq.keys=keysin
fyq.zhuanhua()
else:
print("字符串不合法!")
os.system("pause")
continue
if(wordin=="keepgoing"):
a=" "+ans
else:
a=""
while(1):
b=input("请输入待加密的字符串")
if(b=="end"):
break
else:
a=a+"\n"+b
a=a[1:]
ans=fyq.encode(a)
#输出
print("加密结果:\n")
print(ans)
#解密
if(mode==2):
#输入
wordin=input("请输入附加命令")
if(wordin=="exitit"):
mode=0
continue
elif(wordin=="setkey"):
keysin=input("请输入使用的字符")
if(len(keysin)==4):
fyq.keys=keysin
fyq.zhuanhua()
else:
print("字符串不合法!")
os.system("pause")
continue
if(wordin=="keepgoing"):
a=ans
else:
a=""
while(1):
b=input("请输入待解密的字符串")
if(b=="end"):
break
else:
a=a+b
ans=fyq.decode(a)
#输出
print("解密结果:")
print(ans)
#设置
if(mode==3):
mode=0
commandin=input("请输入设置的内容")
if(commandin=="password"):
fyq.password=int(input("请自觉输入合法(0~65535)的十进制/十六进制数"))
fyq.zhuanhua()
elif(commandin=="keys"):
keysin=input("请输入使用的字符")
if(len(keysin)==4):
fyq.keys=keysin
fyq.zhuanhua()
else:
print("字符串不合法!")
mode=0
continue
else:
print("无此设置项!")
#帮助
if(mode==4):
mode=0
print('''
帮助列表
主菜单:
encode 加密通话
decode 语言解密
set 设置
ans 上一次运算结果
about 关于
Back to home |
File page
Subscribe |
Register |
Login
| N