Posted in Python onApril 12, 2018
本文实例讲述了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)
二 运行结果:
Python实现的维尼吉亚密码算法示例
- Author -
chengqiuming声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@