本文实例讲述了Python实现的维尼吉亚密码算法。分享给大家供大家参考,具体如下:
一 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# -*- coding:utf-8 -*-
#key='relations'
#plaintext='tomorrowiwillhaveagood'
print ( "服务器之家测试结果:" )
key = 'helloworld'
plaintext = raw_input ( '请输入明文:' )
ascii = 'abcdefghijklmnopqrstuvwxyz'
keylen = len (key)
ptlen = len (plaintext)
ciphertext = ''
i = 0
while i < ptlen:
j = i % keylen
k = ascii.index(key[j])
m = ascii.index(plaintext[i])
ciphertext + = ascii[(m + k) % 26 ]
i + = 1
print (ciphertext)
#维吉尼亚加密算法 解密
key = 'helloworld'
ciphertext = raw_input ( '请输入密文:' )
ascii = 'abcdefghijklmnopqrstuvwxyz'
keylen = len (key)
ctlen = len (ciphertext)
plaintext = ''
i = 0
while i < ctlen:
j = i % keylen
k = ascii.index(key[j])
m = ascii.index(ciphertext[i])
if m < k:
m + = 26
plaintext + = ascii[m - k]
i + = 1
print (plaintext)
|
二 运行结果:
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/chengqiuming/article/details/78601100