凯撒加解密程序
class Caesar:
def __init__(self,key=3):
self.__key=key
self.__code= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz" \
"1234567890~!@#$%&*()_+=[]{}|\\;:\'\"< >,.?/"
def getdectext(self,ciptext):
cleartext=""
a=0
for i in ciptext:
if i in self.__code:
a=(self.__code.find(i)-self.__key)%len(self.__code)
cleartext+=self.__code[a]
else:
cleartext+=i
return cleartext
def getenctext(self,cleartext):
ciptext=""
a=0
for i in cleartext:
if i in self.__code:
a=(self.__code.find(i)+self.__key)%len(self.__code)
ciptext+=self.__code[a]
else:
ciptext+=i
return ciptext
def getkey(self):
return self.__key
def setkey(self,key):
self.__key=key
def getcode(self):
return self.__key
def setcode(self,code):
self.__code=code
def main():
cleartext=input("请输入明文:")
key=eval(input("请输入密钥(一个整数):"))
c=Caesar(key)
ciptext=c.getenctext(cleartext)
print("明文:",cleartext)
print("密文:",ciptext)
text=c.getdectext(ciptext)
print("解密:",text)
import time
starttime=time.time()
main()
endtime=time.time()
usetime=endtime-starttime
print('所用时间',int(usetime))
哈希加密程序(MD5 算法和 SHA-256 算法)
class Encrypt:
def __init__(self):
self.salt1 = 'qwe1234tyr'
self.salt2 = 'qwerty6834'
def md5(self, message):
m = hashlib.md5()
m.update(self.salt1.encode(encoding='utf-8'))
m.update(message.encode(encoding='utf-8'))
return m.hexdigest()
def sha256(self, message):
hash = hashlib.sha256(self.salt2.encode('utf-8'))
hash.update(message.encode('utf-8'))
return hash.hexdigest()
import hashlib
e = Encrypt()
message = input("请输入明文:")
cipertext1 = e.md5(message)
print('md5加密:'+cipertext1)
cipertext2 = e.sha256(message)
print('sha256加密'+cipertext2)