python應用_愷撒密碼加密與解密,python加密與解密

來源:互聯網
上載者:User

python應用_愷撒密碼加密與解密,python加密與解密

1.它是一種替換加密的技術,明文中的所有字母都在字母表上向後(或向前)按照一個固定數目進行位移後被替換成密文。例如,當位移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當年愷撒曾用此方法與其將軍們進行聯絡。

2.ASCII表:


2.具體實現:

class Caesar:    def __init__(self,a,b):#初始化,接受程式需要的類型        self.pass1=a        self.type1=b    def jiami(self,a):#加密的程式        z=(ord(a)-97+self.pass1)%26+97#用ascii碼值來完成移動        return chr(z)    def jiemi(self,a):#解密的程式        z=(ord(a)-97-self.pass1)%26+97        if z<97:            z=z+26        return chr(z)    def show(self,x):#顯示結果        str=''        if self.type1==0:#加密的時候進入            for i in range (len(x)):                x=x[:i]+self.jiami(x[i])+x[i+1:]            for i in range (len(x)):                str=str+x[i]            print (str)        else:#解密的時候進入            for i in range (len(x)):                x=x[:i]+self.jiemi(x[i])+x[i+1:]            for i in range (len(x)):                str=str+x[i]            print (str)if __name__=='__main__':#測試程式    a=int(input('please input the pass (小於26!0:結束):'))    while a:        b=int(input('please input the type (0:加密;1:解密:)'))        x=input('please input str:')        user=Caesar(a,b)        user.show(x)        a=int(input('please input the pass (小於26!0:結束):'))

3.實驗結果:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.