你静候着秋天的温柔,留我在风景之外。把眼睛留给风光,把体重留给美食。我坐在椅子上,看日出复活;我坐在夕阳里,看城市的衰落。
本文实例讲述了Python实现的维尼吉亚密码算法。分享给大家供大家参考,具体如下:
一 代码
# -*- 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)
二 运行结果:
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.haodaima.com/password/txt_encode
MD5在线加密工具:
http://tools.haodaima.com/password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools.haodaima.com/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.haodaima.com/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.haodaima.com/password/sha_encode
希望本文所述对大家Python程序设计有所帮助。
本文Python实现的维尼吉亚密码算法示例到此结束。自我要先看得起自我,别人才会看得起你。小编再次感谢大家对我们的支持!