python實(shí)現(xiàn)移位加密和解密
本文實(shí)例為大家分享了python實(shí)現(xiàn)移位加密和解密的具體代碼,供大家參考,具體內(nèi)容如下
代碼很簡單,就不多做解釋啦。主要思路是將字符串轉(zhuǎn)為Ascii碼,將大小寫字母分別移位密鑰表示的位數(shù),然后轉(zhuǎn)回字符串。需要注意的是,當(dāng)秘鑰大于26的時候,我使用循環(huán)將其不斷減去26,直到密鑰等效小于26為止。
def encrypt(): temp = raw_input("Please input your sentence: ") key = int(raw_input("Please input your key: ")) listA = map(ord,temp) lens = len(listA) for i in range(lens): a = listA[i] if 65 <= a <= 90: a += key while a > 90: a -= 26 elif 97 <= a <= 122: a += key while a > 122: a -= 26 listA[i] = a listA = map(chr,listA) listA = ''.join(listA) print listA def unencrypt(): temp = raw_input("Please input your sentence: ") key = int(raw_input("Please input your key: ")) listA = map(ord, temp) lens = len(listA) for i in range(lens): a = listA[i] if 65 <= a <= 90: a -= key while a < 65: a += 26 elif 97 <= a <= 122: a -= key while a < 97: a += 26 listA[i] = a listA = map(chr, listA) listA = ''.join(listA) print listA a = int(raw_input("input 0 to encrypt and 1 to unencrypt")) if a == 0: encrypt() elif a == 1: unencrypt()
效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何使用matplotlib讓你的數(shù)據(jù)更加生動
數(shù)據(jù)可視化用于以更直接的表示方式顯示數(shù)據(jù),并且更易于理解,下面這篇文章主要給大家介紹了關(guān)于如何使用matplotlib讓你的數(shù)據(jù)更加生動的相關(guān)資料,需要的朋友可以參考下2021-11-11詳解運(yùn)行Python的神器Jupyter Notebook
如果我們想要運(yùn)行Python,就是在Python或者IPython的解釋器環(huán)境中進(jìn)行交互式運(yùn)行,或者程序員最喜歡的編寫.py文件,在文件中編寫python代碼,然后運(yùn)行。如果想寫一篇Python的文章,里面有代碼,還希望代碼在當(dāng)前頁面運(yùn)行,那就是使用我們今天要介紹的Jupyter Notebook。2021-06-06Python基于Tkinter模塊實(shí)現(xiàn)的彈球小游戲
這篇文章主要介紹了Python基于Tkinter模塊實(shí)現(xiàn)的彈球小游戲,涉及Python圖形繪制、數(shù)值計(jì)算、判斷等相關(guān)操作技巧,需要的朋友可以參考下2018-12-12使用python根據(jù)端口號關(guān)閉進(jìn)程的方法
今天小編就為大家分享一篇使用python根據(jù)端口號關(guān)閉進(jìn)程的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11Pytorch數(shù)據(jù)讀取與預(yù)處理該如何實(shí)現(xiàn)
這篇文章主要介紹了Pytorch數(shù)據(jù)讀取與預(yù)處理該如何實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Pytorch,感興趣的朋友可以了解下2021-03-03