目录
仿射密码
Affine Cipher是Multiplicative Cipher和Caesar Cipher算法的组合.
仿射密码的基本实现如下图所示 :
我们将通过创建包含两个加密和解密基本函数的相应类来实现仿射密码.
代码
您可以使用以下代码实现仿射密码 :
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\')
输出
实现仿射密码时,可以观察到以下输出;
输出显示纯文本消息仿射密码的加密消息和已作为输入 abcdefg发送的消息的解密消息.
单字母密码
接下来,您将学习使用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\', \' \': \' \', }
借助此词典,我们可以使用相关字母加密字母为JSON对象中的值.
以下程序创建一个单字母程序作为类表示,其中包括加密和解密的所有功能.
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) )
稍后调用此文件以实现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)
输出
当您实现上面给出的代码时,您可以观察到以下输出;
T嗯,你可以用一个指定的键值对来破解单字母密码,这会将密文破解成实际的纯文本.
以上就是Python密码学仿射密码及攻击单字母密码教程的详细内容,更多关于Python仿射攻击单字母密码的资料请关注其它相关文章!
© 版权声明
THE END
暂无评论内容