凯撒密码源码(python)

'''
        凯撒密码
        @auhor 郭财德 蔡泽雄
        @time 2019/01/20
'''
#编码对照表
LETTERSI = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
LETTERSII = 'abcdefghijklmnopqrstuvwxyz'

class Caetsar():
    '''
            Encrypt: 加密方法
            Decrypt: 解密方法
    '''
    def __init__(self):
        pass


    def Encrypt(self, string="", x=0, blank=' '):
        '''
            对输入的字符串进行加密
            args={
                string: 用于加密的字符串
                mode: 是否区分大小写,默认全为小写
                blank: 密文间隔符号
            }
        '''
        print("密文:"+string)
        if x == 0:
            self.mstr = string.lower()
        else:
            self.mstr = string
        self.cstr = ''
        print('请输入加密的密钥(0~26):')
        key = input()
        for symble in self.mstr:
            if symble in LETTERSII:
                num = LETTERSII.find(symble)
                num = num + int(key)
                if num >=len(LETTERSI):
                        num = num - len(LETTERSII)
                self.cstr = self.cstr + LETTERSII[num]
            else:
                self.cstr = self.cstr + symble
        self.Output("加密结果:"+self.cstr)
        
     
    def Decrypt(self, string="", mode=0, x=0, blank=' '):

        '''
            对输入的字符串进行解密
            args={
                string: 用于解密的密文字符串
                mode: 说明解密模式,默认为暴力破解
                x: 是否区分大小写, 默认全为小写
                blank: 密文间隔符号
            }
        '''
        print("密文:"+string)
        if x == 0:
            self.cstr = string.lower()
        else:
            self.cstr = string
	#对第一种模式解密
        if mode == 0:
            print("暴力破解解密结果:")
            for key in range(26):
                self.mstr1 = ""
                for symble in self.cstr:    
                    if symble in LETTERSI:
                        num = LETTERSI.find(symble)
                        num = num - key
                        if num < 0:
                            num = num + 26
                        self.mstr1 = self.mstr1 + LETTERSI[num]
                    elif symble in LETTERSII:
                        num = LETTERSII.find(symble)
                        num = num - key
                        if num < 0:
                            num = num + 26
                        self.mstr1 = self.mstr1 + LETTERSII[num]
                    else:
                        self.mstr1 = self.mstr1 + symble
                print('key #%-2d: %s'%(key,self.mstr1))
        #对第二种模式解密
        elif mode == 1:
            self.mstr2 = ""
            print('请输入加密的密钥(0~26):')
            key = input()
            for symble in self.cstr:   
                if symble in LETTERSI:
                    num = LETTERSI.find(symble)  
                    num = num - int(key)      
                    if num < 0:
                        num = num + len(LETTERSI)
                    self.mstr2 = self.mstr2 + LETTERSI[num]
                elif symble in LETTERSII:
                    num = LETTERSII.find(symble)  
                    num = num - int(key)    
                    if num >= len(LETTERSII):
                        num = num - len(LETTERSII)
                    self.mstr2 = self.mstr2 + LETTERSII[num]
                else:
                    self.mstr2 = self.mstr2 + symble   
            self.Output("密钥解密结果:"+self.mstr2)

        else:
            Output("Error mode!")

            
    def Output(self, strings):
        '''
                作用:输出数据
		args={
		    strings:将输出的数据
		}
	    '''
        print(strings)



if __name__ == '__main__':
    C = Caetsar()
    C.Encrypt("abcDEF", x=0)
    C.Decrypt("bcdEFG", mode=1, x=0)

你可能感兴趣的:(凯撒密码源码(python))