通过编程实现替代密码算法和置换密码算法,加深对古典密码体制的了解

时间: 2023-11-10 13:01:46 浏览: 64
替代密码算法: ```python # 替代密码算法 def substitution_cipher(plaintext, key): # 构建密文字典 ciphertext_dict = {} for i in range(len(key)): ciphertext_dict[chr(97+i)] = key[i] # 加密明文 ciphertext = '' for c in plaintext: if c.isalpha(): if c.isupper(): ciphertext += ciphertext_dict[c.lower()].upper() else: ciphertext += ciphertext_dict[c] else: ciphertext += c return ciphertext ``` 示例: ```python plaintext = 'hello world' key = 'qwertyuiopasdfghjklzxcvbnm' ciphertext = substitution_cipher(plaintext, key) print(ciphertext) # lazzq zphqw ``` 置换密码算法: ```python # 置换密码算法 def transposition_cipher(plaintext, key): # 根据密钥计算列数 col_num = len(key) # 根据明文长度计算行数 row_num = (len(plaintext) + col_num - 1) // col_num # 在明文末尾填充空格 plaintext += ' ' * (row_num * col_num - len(plaintext)) # 构建密文矩阵 matrix = [] for i in range(row_num): row = [plaintext[i*col_num+j] for j in range(col_num)] matrix.append(row) # 按照密钥的字母顺序排序列 sorted_index = sorted(range(col_num), key=lambda k: key[k]) # 按照排序后的列顺序输出密文 ciphertext = '' for j in sorted_index: for i in range(row_num): ciphertext += matrix[i][j] return ciphertext ``` 示例: ```python plaintext = 'hello world' key = 'helo wrd' ciphertext = transposition_cipher(plaintext, key) print(ciphertext) # hleolwolrd ```

相关推荐

最新推荐

recommend-type

python 密码学示例——理解哈希(Hash)算法

主要介绍了哈希(Hash)算法的相关资料,帮助大家更好的利用python处理密码,感兴趣的朋友可以了解下
recommend-type

信息安全技术实验五古典密码算法

1.古典密码算法之 替代算法 import java.io.*; public class Caesar { //向右加密方法 ming为明文 mi 为密文 yao为密钥 public static void encode(String ming, int yao) { String mi = ""; for(int i=0; i()...
recommend-type

Java编程实现A*算法完整代码

主要介绍了Java编程实现A*算法完整代码,简单介绍了a星算法,然后分享了完整测试代码,具有一定借鉴价值,需要的朋友可以参考下。
recommend-type

广州大学 数据结构实验报告 实验四 查找和排序算法实现

实验四 查找和排序算法实现 1、各种排序算法的实现 2、各种查找算法实现 1、各种排序算法的实现 用随机函数生成16个2位正整数(10~99),实现插入排序、选择排序、冒泡排序、双向冒泡、快速排序、二路归并排序等多种...
recommend-type

Java编程实现轨迹压缩之Douglas-Peucker算法详细代码

主要介绍了Java编程实现轨迹压缩之Douglas-Peucker算法详细代码,具有一定借鉴价值,需要的朋友可以参考。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。