维吉尼亚密码

维吉尼亚密码属于多表代换密码

其中A<-->0,B<-->1,...,Z<-->25,则每个密钥K相当于一个长度为m的字母串,称为密钥字。维吉尼亚密码一次加密m个明文字母。

示例:设m=6,密钥字为"CIPHER",对应数字串K=(2,8,15,7,4,17)。要加密明文为:thiscrypto,则
则相应的密文为:VPXZGIAXIV

解密时,使用相同的密钥字进行逆运算即可。

可以看出,维吉尼亚密码的密钥空间为26^m。

加密:

python 复制代码
Plaintext = 'THISCRYPTO'#字母需要大写
K = [2,8,15,7,4,17]

#print(K)#密钥

Ciphertext = ''
for i in range(len(Plaintext)):
    M_i = ord(Plaintext[i]) - 65 
    K_i = K[i%len(K)]
    C_i = (M_i+K_i)%26
    Ciphertext += chr(C_i+ 65) 
print(Ciphertext)

解密:

python 复制代码
Ciphertext = 'VPXZGIAXIV'   
K = [2,8,15,7,4,17]

Plaintext = ''
for i in range(len(Ciphertext)):
    C_i = ord(Ciphertext[i]) -65 
    K_i = K[i%len(K)]
    M_i = (C_i - K_i)%26
    Plaintext += chr(M_i+ 65) 
print(Plaintext)
相关推荐
前端梭哈攻城狮29 分钟前
dify二开示例
前端·后端·python
秋难降33 分钟前
一篇文章带你了解Pandassssssssssssssss
大数据·python·pandas
java1234_小锋36 分钟前
[免费]【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts)【论文+源码+SQL脚本】
python·flask·nlp·舆情分析·微博舆情分析
weixin_贾40 分钟前
模块自由拼装!Python重构DSSAT作物模块教程(以杂交水稻为例)
python·dssat
Warren981 小时前
Java Collections工具类
java·开发语言·笔记·python·学习·oracle·硬件工程
love530love2 小时前
Windows 11 下 Anaconda 命令修复指南及常见问题解决
运维·ide·人工智能·windows·python·架构·conda
NeoFii2 小时前
Day 24:元组与os模块
python·机器学习
半新半旧2 小时前
1.DRF 环境安装与配置
python·django
Sean_summer2 小时前
暑期第二周
前端·数据库·python
封奚泽优2 小时前
使用Python绘制金融数据可视化工具
python·信息可视化·excel·pandas·pyplot·qtwidgets·qtcore