多码加密 vigenere算法 python 实现
来源:互联网 发布:怎样查看淘宝销售数据 编辑:程序博客网 时间:2024/04/28 23:14
转自:http://blog.csdn.net/woshiaotian/article/details/18038391
基于我自己对 vigenere 的理解,另外vigenere 属于非常弱的一种加密,用于生产环境不是非常安全请注意
- # -*- coding:utf-8 -*-
- ##################################
- # Vigenere 是一种多码加密法
- # author vearne
- # ***注意***:
- # 1) 字母表中必须要包含明文中出现的字母
- # 2) 密钥不能为空
- #
- ##################################
- class Vigenere(object):
- def __init__(self, table='0123456789', key='apple'):
- # 字母表
- self.table = table
- # 密钥
- self.key = key
- def genNum(self, curr):
- if curr + 1 >= len(self.key):
- return 0
- else:
- return curr + 1
- def dict(self, chr, move):
- index = self.table.index(chr)
- return self.table[(index + move) % len(self.table)]
- def encrypt(self, cleartext):
- # i 指向明文, j 指向密钥
- j = 0
- ll = []
- for i in range(len(cleartext)):
- move = ord(self.key[j]) % len(self.table)
- # print 'move', move
- new_chr = self.dict(cleartext[i], move)
- ll.append(new_chr)
- j = self.genNum(j)
- return ''.join(ll)
- def decrypt(self, ciphertext):
- # i 指向密文, j 指向密钥
- j = 0
- ll = []
- for i in range(len(ciphertext)):
- move = ord(self.key[j]) % len(self.table)
- move = move * (-1)
- # print 'move', move
- new_chr = self.dict(ciphertext[i], move)
- ll.append(new_chr)
- j = self.genNum(j)
- return ''.join(ll)
- if __name__ == '__main__':
- v = Vigenere(key='apple077226')
- cleartext = '000000668'
- print cleartext
- ciphertext = v.encrypt(cleartext)
- print ciphertext
- print '----------------------------------'
- cleartext = v.decrypt(ciphertext)
- print cleartext
如果字母表中的字母出现不重复,则可以保证明文跟密文的一一映射,如果出现重复,则会出现明文跟密文的多对一映射。
打乱字母表中字母的顺序,可以使密文更具有欺骗性。
0 0
- 多码加密 vigenere算法 python 实现
- 多码加密 vigenere算法 python 实现
- Vigenere密码 Python实现
- Vigenere加密解密C++实现
- Vigenere多表加法加密分析
- Vigenere加密法C++
- Vigenere加密问题
- (3)Vigenere密码算法的Java实现
- Vigenere密码加密解密原理
- DES加密解密算法之python实现版
- 聊胜于无 Java之Caesar与Vigenere实现
- 聊胜于无 Java之Caesar与Vigenere实现
- 古典密码学(Vigenere)之Java实现
- python实现凯撒加密
- python实现MD5加密
- python实现MD5加密
- python实现rsa加密
- python实现凯撒加密
- “合”“和”共赢
- 出现次数
- ACM HDOJ 1151 (Air Raid)
- 黑马程序员_面向对象1
- 黑马程序员--Java基础--01函数
- 多码加密 vigenere算法 python 实现
- CY7C68013A Slave FIFO 相关的寄存器说明
- Raw格式研究(更新中。。。)
- 【c++】cannot declare member function *** to have static linkage [-fpermissive]
- 关于web服务器的性能测试实验
- C# 学习教程七
- apache更改端口后无法启动成功的解决方案
- [NCE2]Lesson 3. Please send me a card.
- arm-linux-gcc compile the cross-platform mpich2 on ubuntu