python密碼學(xué)簡單替代密碼解密及測試教程
簡單替代密碼
簡單替換密碼是最常用的密碼,包括為每個密文文本字符替換每個純文本字符的算法.在這個過程中,與凱撒密碼算法相比,字母表是混亂的.
示例
簡單替換密碼的密鑰通常由26個字母組成.一個示例鍵是 :
plain?alphabet?:?abcdefghijklmnopqrstuvwxyz cipher?alphabet:?phqgiumeaylnofdxjkrcvstzwb
使用上述密鑰的示例加密是 :
plaintext?:?defend?the?east?wall?of?the?castle ciphertext:?giuifg?cei?iprc?tpnn?du?cei?qprcni
以下代碼顯示了一個實現(xiàn)簡單替換密碼的程序;
import?random,?sys
LETTERS?=?'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
def?main():
???message?=?''
???if?len(sys.argv)?>?1:
??????with?open(sys.argv[1],?'r')?as?f:
?????????message?=?f.read()
???else:
??????message?=?raw_input("Enter?your?message:?")
???mode?=?raw_input("E?for?Encrypt,?D?for?Decrypt:?")
???key?=?''
???
???while?checkKey(key)?is?False:
??????key?=?raw_input("Enter?26?ALPHA?key?(leave?blank?for?random?key):?")
??????if?key?==?'':
?????????key?=?getRandomKey()
??????if?checkKey(key)?is?False:
print('There?is?an?error?in?the?key?or?symbol?set.')
???translated?=?translateMessage(message,?key,?mode)
???print('Using?key:?%s'?%?(key))
???
???if?len(sys.argv)?>?1:
??????fileOut?=?'enc.'?+?sys.argv[1]
??????with?open(fileOut,?'w')?as?f:
?????????f.write(translated)
??????print('Success!?File?written?to:?%s'?%?(fileOut))
???else:?print('Result:?'?+?translated)
#?Store?the?key?into?list,?sort?it,?convert?back,?compare?to?alphabet.
def?checkKey(key):
???keyString?=?''.join(sorted(list(key)))
???return?keyString?==?LETTERS
def?translateMessage(message,?key,?mode):
???translated?=?''
???charsA?=?LETTERS
???charsB?=?key
???
???#?If?decrypt?mode?is?detected,?swap?A?and?B
???if?mode?==?'D':
??????charsA,?charsB?=?charsB,?charsA
???for?symbol?in?message:
??????if?symbol.upper()?in?charsA:
?????????symIndex?=?charsA.find(symbol.upper())
?????????if?symbol.isupper():
????????????translated?+=?charsB[symIndex].upper()
?????????else:
????????????translated?+=?charsB[symIndex].lower()
else:
???????????????translated?+=?symbol
?????????return?translated
def?getRandomKey():
???randomList?=?list(LETTERS)
???random.shuffle(randomList)
???return?''.join(randomList)
if?__name__?==?'__main__':
???main()輸出
您可以觀察以下內(nèi)容當(dāng)你實現(xiàn)上面給出的代碼時輸出 :

簡單替換密碼測試
我們將重點介紹如何使用各種方法測試替換密碼,這有助于生成隨機字符串,如下面所示 :
import?random,?string,?substitution
def?main():
???for?i?in?range(1000):
??????key?=?substitution.getRandomKey()
??????message?=?random_string()
??????print('Test?%s:?String:?"%s.."'?%?(i?+?1,?message[:50]))
??????print("Key:?"?+?key)
??????encrypted?=?substitution.translateMessage(message,?key,?'E')
??????decrypted?=?substitution.translateMessage(encrypted,?key,?'D')
??????
??????if?decrypted?!=?message:
?????????print('ERROR:?Decrypted:?"%s"?Key:?%s'?%?(decrypted,?key))
?????????sys.exit()
??????print('Substutition?test?passed!')
def?random_string(size?=?5000,?chars?=?string.ascii_letters?+?string.digits):
???return?''.join(random.choice(chars)?for?_?in?range(size))
if?__name__?==?'__main__':
???main()輸出
您可以隨機觀察輸出生成的字符串有助于生成隨機純文本消息,如下所示 :

測試成功完成后,我們可以觀察輸出消息替換測試通過!.

因此,您可以系統(tǒng)地破解替換密碼.
簡單替換密碼解密
您可以了解替換密碼的簡單實現(xiàn),它根據(jù)簡單替換密碼技術(shù)中使用的邏輯顯示加密和解密的消息.這可以被視為一種替代編碼方法.
代碼
您可以使用以下代碼使用簡單替換密碼來執(zhí)行解密;
import?random
chars?=?'ABCDEFGHIJKLMNOPQRSTUVWXYZ'?+?\
???'abcdefghijklmnopqrstuvwxyz'?+?\
???'0123456789'?+?\
???':.;,?!@#$%&()+=-*/_<>?[]{}`~^"\'\\'
def?generate_key():
???"""Generate?an?key?for?our?cipher"""
???shuffled?=?sorted(chars,?key=lambda?k:?random.random())
???return?dict(zip(chars,?shuffled))
def?encrypt(key,?plaintext):
???"""Encrypt?the?string?and?return?the?ciphertext"""
???return?''.join(key[l]?for?l?in?plaintext)
def?decrypt(key,?ciphertext):
???"""Decrypt?the?string?and?return?the?plaintext"""
???flipped?=?{v:?k?for?k,?v?in?key.items()}
???return?''.join(flipped[l]?for?l?in?ciphertext)
def?show_result(plaintext):
???"""Generate?a?resulting?cipher?with?elements?shown"""
???key?=?generate_key()
???encrypted?=?encrypt(key,?plaintext)
???decrypted?=?decrypt(key,?encrypted)???
???print?'Key:?%s'?%?key
print?'Plaintext:?%s'?%?plaintext
???print?'Encrypted:?%s'?%?encrypted
???print?'Decrypted:?%s'?%?decrypted
show_result('Hello?World.?This?is?demo?of?substitution?cipher')以上就是python密碼學(xué)簡單替代密碼解密及測試教程的詳細(xì)內(nèi)容,更多關(guān)于python替代密碼解密測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python numpy實現(xiàn)多次循環(huán)讀取文件 等間隔過濾數(shù)據(jù)示例
這篇文章主要介紹了python numpy實現(xiàn)多次循環(huán)讀取文件 等間隔過濾數(shù)據(jù)示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
利用python實現(xiàn)詞頻統(tǒng)計分析的代碼示例
詞頻統(tǒng)計是指在文本或語音數(shù)據(jù)中,統(tǒng)計每個單詞或符號出現(xiàn)的次數(shù),以便對文本或語音數(shù)據(jù)進(jìn),這篇文章將詳細(xì)介紹分詞后如何進(jìn)行詞頻統(tǒng)計分析2023-06-06
合并百度影音的離線數(shù)據(jù)( with python 2.3)
這篇文章主要介紹了合并百度影音的離線數(shù)據(jù)( with python 2.3)的相關(guān)資料2015-08-08
pytorch常用數(shù)據(jù)類型所占字節(jié)數(shù)對照表一覽
這篇文章主要介紹了pytorch常用數(shù)據(jù)類型所占字節(jié)數(shù)對照表一覽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
在Python的web框架中編寫創(chuàng)建日志的程序的教程
這篇文章主要介紹了在Python的web框架中編寫創(chuàng)建日志的程序的教程,示例代碼基于Python2.x版本,需要的朋友可以參考下2015-04-04
python判斷一個數(shù)是否能被另一個整數(shù)整除的實例
今天小編就為大家分享一篇python判斷一個數(shù)是否能被另一個整數(shù)整除的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12

