Python密碼學(xué)仿射密碼及攻擊單字母密碼教程
仿射密碼
Affine Cipher是Multiplicative Cipher和Caesar Cipher算法的組合.
仿射密碼的基本實現(xiàn)如下圖所示 :
我們將通過創(chuàng)建包含兩個加密和解密基本函數(shù)的相應(yīng)類來實現(xiàn)仿射密碼.
代碼
您可以使用以下代碼實現(xiàn)仿射密碼 :
?class仿射(對象): ?DIE?=?128? ?KEY?=(7,3,55) ?def?__init?__(self): #傳遞 ?def?encryptChar(self,char): ?K1,K2,?kI?=?self.KEY? ?return?chr((K1?*?ord(char)+?K2)%self.DIE) ?def?encrypt(self,string): ?return""?.join(map(self.encryptChar,string)) ?def?decryptChar(self,char): ?K1,K2,KI?=?self.KEY? ?return?chr(KI?*?(ord(char)?-??K2)%self.DIE) ?def?decrypt(self,string): ?return"".join(map(self.decryptChar,string)) ?affine?=?Affine() ?print?affine.encrypt('?Affine?Cipher') ?print?affine.decrypt('*?18?FMT')
輸出
實現(xiàn)仿射密碼時,可以觀察到以下輸出;
輸出顯示純文本消息仿射密碼的加密消息和已作為輸入 abcdefg發(fā)送的消息的解密消息.
單字母密碼
接下來,您將學(xué)習(xí)使用Python的單字母密碼及其黑客攻擊.
單字母密碼使用固定替換用于加密整個消息.這里顯示使用帶有JSON對象的Python字典的單字母密碼 :
monoalpha_cipher?=?{ ???'a':?'m', ???'b':?'n', ???'c':?'b', ???'d':?'v', ???'e':?'c', ???'f':?'x', ???'g':?'z', ???'h':?'a', ???'i':?'s', ???'j':?'d', ???'k':?'f', ???'l':?'g', ???'m':?'h', ???'n':?'j', ???'o':?'k', ???'p':?'l', ???'q':?'p', ???'r':?'o', ???'s':?'i', ???'t':?'u', ???'u':?'y', ???'v':?'t', ???'w':?'r', ???'x':?'e', ???'y':?'w', ???'z':?'q', '?':?'?', }
借助此詞典,我們可以使用相關(guān)字母加密字母為JSON對象中的值.
以下程序創(chuàng)建一個單字母程序作為類表示,其中包括加密和解密的所有功能.
from?string?import?letters,?digits from?random?import?shuffle def?random_monoalpha_cipher(pool?=?None): ???if?pool?is?None: ??????pool?=?letters?+?digits ???original_pool?=?list(pool) ???shuffled_pool?=?list(pool) ???shuffle(shuffled_pool) ???return?dict(zip(original_pool,?shuffled_pool)) def?inverse_monoalpha_cipher(monoalpha_cipher): ???inverse_monoalpha?=?{} ???for?key,?value?in?monoalpha_cipher.iteritems(): ??????inverse_monoalpha[value]?=?key ???return?inverse_monoalpha def?encrypt_with_monoalpha(message,?monoalpha_cipher): ???encrypted_message?=?[] ???for?letter?in?message: ??????encrypted_message.append(monoalpha_cipher.get(letter,?letter)) ???return?''.join(encrypted_message) def?decrypt_with_monoalpha(encrypted_message,?monoalpha_cipher): ???return?encrypt_with_monoalpha( ??????encrypted_message, ??????inverse_monoalpha_cipher(monoalpha_cipher) ???)
稍后調(diào)用此文件以實現(xiàn)Monoalphabetic密碼的加密和解密過程,如下所示 :
import?monoalphabeticCipher?as?mc cipher?=?mc.random_monoalpha_cipher() print(cipher) encrypted?=?mc.encrypt_with_monoalpha('Hello?all?you?hackers?out?there!',?cipher) decrypted?=?mc.decrypt_with_monoalpha('sXGGt?SGG?Nt0?HSrLXFC?t0U?UHXFX!',?cipher) print(encrypted) print(decrypted)
輸出
當(dāng)您實現(xiàn)上面給出的代碼時,您可以觀察到以下輸出;
T嗯,你可以用一個指定的鍵值對來破解單字母密碼,這會將密文破解成實際的純文本.
以上就是Python密碼學(xué)仿射密碼及攻擊單字母密碼教程的詳細(xì)內(nèi)容,更多關(guān)于Python仿射攻擊單字母密碼的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Python 重學(xué)requests發(fā)起請求的基本方式
這篇文章主要介紹了詳解Python 重學(xué)requests發(fā)起請求的基本方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例
這篇文章主要介紹了Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例,通過在一個表格中進行操作來論述如何使用Python快速安全地操作sqlite3,需要的朋友可以參考下2014-04-04pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作
今天小編就為大家分享一篇pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02如何在Python?中使用?join()?函數(shù)把列表拼接成一個字符串
這篇文章主要介紹了如何在Python?中使用?join()?函數(shù)把列表拼接成一個字符串,文章圍繞?join()?函數(shù)的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對你有幫助2022-03-03