Python仿射密码的实现和单字母密码用法是什么
Admin 2022-08-23 群英技术资讯 631 次浏览
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中temp是什么?根据操作的过程进行临时保存的文件技术。python中临时文件及文件夹使用。使用的是tempfile包
有的时候我们写的东西不想让别人看到,会设置密码来加密。在Python中,我们写的字符字符不想被别人看到,保护自己的隐私,会选择加密。本文介绍Python字符串加密的五种方法:url编码、base64、ascii、md5 Unicode转中文。具体代码如下:
在处理数据的时候,很多时候会遇到批量替换的情况,如果一个一个去修改效率过低,也容易出错,replace()是很好的方法,下面这篇文章主要给大家介绍了关于Python pandas.replace用法的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Python通过ffmpeg实现修改图片分辨率,文中的代码介绍详细,对我们的工作或学习有一定的价值,感兴趣的小伙伴可以学习一下
PyTorch是一个开源的Python机器学习库,基于Torch,用于自然语言处理等应用程序。它具有强大的GPU加速的张量计算和自动求导系统的深度神经网络。本文将介绍如何通过PyTorch实现病虫害图像分类,感兴趣的可以学习一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008