Python仿射密码的实现和单字母密码用法是什么
Admin 2022-08-23 群英技术资讯 861 次浏览
今天这篇我们来学习和了解“Python仿射密码的实现和单字母密码用法是什么”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“Python仿射密码的实现和单字母密码用法是什么”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!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嗯,你可以用一个指定的键值对来破解单字母密码,这会将密文破解成实际的纯文本.
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了基于Python和PyQT5实现简易的文档格式转换器,支持.txt/.xlsx/.csv格式的转换。感兴趣的小伙伴可以跟随小编一起学习一下
Python 海象运算符是在 PEP 572 中提出,并在 Python3.8 版本并入和发布。本文就来为大家详细讲讲Python海象运算符的用法,感兴趣的可以了解一下
Python内置函数-len()函数。python len() 方法返回对象(字符、列表、元组等)长度或项目个数。
struct模块执行Python值和以Python bytes表示的C结构体之间的转换,这可以用于处理存储在文件中或来自网络连接以及其他源的二进制数据,下面介绍下Python使用struct库的用法,感兴趣的朋友一起看看吧
Matplotlib是Python的绘图库,它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。本文将为大家介绍如何用matplotlib绘制灰度和彩色直方图,感兴趣的朋友可以学习一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008