Python仿射密码的实现和单字母密码用法是什么
Admin 2022-08-23 群英技术资讯 1546 次浏览
今天这篇我们来学习和了解“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二叉树类以及其4种遍历方法的相关资料,需要的朋友可以参考下
这篇文章主要weidajia 介绍了python目标检测非极大抑制NMS与Soft-NMS实现过程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
这篇文章主要介绍了Python实现Telnet自动连接检测密码的示例,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
本篇文章给大家带来了关于Python的相关知识,logging是Python标准库中记录常用的记录日志库,通过logging模块存储各种格式的日志,主要用于输出运行日志,可以设置输出日志的等级、日志保
这篇文章主要为大家介绍了python密码学RSA密码解密教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008