Python实现的维尼吉亚密码算法示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现的维尼吉亚密码算法。分享给大家供大家参考,具体如下:

一 代码

# -*- coding:utf-8 -*-
#key='relations'
#plaintext='tomorrowiwillhaveagood'
print("【听图阁-专注于Python设计】测试结果:")
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.jb51.net/password/txt_encode

MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

python实现udp数据报传输的方法

本文实例讲述了Python实现UDP数据报传输的方法,非常具有实用价值。分享给大家供大家参考。具体方法分析如下: 服务端代码: import socket port = 8081...

python 3.6.4 安装配置方法图文教程

python 3.6.4 安装配置方法图文教程

今天补一下关于如何安装Python的操作步骤: 我的系统是我win系统 64 位 1.第一步先去python的官方网站下载python的安装包:地址 根据自己的系统选择对应的...

Python+matplotlib+numpy实现在不同平面的二维条形图

Python+matplotlib+numpy实现在不同平面的二维条形图

在不同平面上绘制二维条形图。 本实例制作了一个3d图,其中有二维条形图投射到平面y=0,y=1,等。 演示结果: 完整代码: from mpl_toolkits.mplot3d...

softmax及python实现过程解析

softmax及python实现过程解析

相对于自适应神经网络、感知器,softmax巧妙低使用简单的方法来实现多分类问题。 功能上,完成从N维向量到M维向量的映射 输出的结果范围是[0, 1],对于一个sample的...

Python中filter与lambda的结合使用详解

filter是Python的内置方法。 官方定义是: filter(function or None, sequence) -> list, tuple, or string...