用python做出下面题目 基于Feistel结构构造一个分组密码算法。要求: 1、 分组长度 = 128 bit 2、 主密钥长度 >= 128 bit 3、 轮数 >= 8 4、 轮函数的设计要用到LFSR 实验二: 实现一个公钥密码算法。要求分别实现如下三个算法: 1、 密钥生成算法 2、 加密算法 3、 解密算法 4、 在RSA和MH中任选一个方案 5、 要求采用大数运算,位数>=256bit 实验三: 利用第一、二次实验的算法,以数字信封的形式对明文文件进行加密。要求如下: 1、 发送方利用系统自带的伪随机数生成函数生成会话密钥 2、 用接收方的公钥对会话密钥加密 3、 用会话密钥以OFB的模式对明文文件p_text.txt(文件大于1KB)进行加密,结果存于密文文件c_text.txt 4、 接收方利用私钥解密会话密钥,然后用会话密钥对密文文件解密,结果存于明文文件p1_text.txt,最后对比p_text.txt和p1_text.txt
时间: 2024-01-13 17:02:41 浏览: 153
以下是基于Feistel结构构造一个分组密码算法、RSA公钥密码算法和数字信封实现的Python示例代码:
Feistel结构构造的分组密码算法实现:
```python
import random
def generate_key(key_len):
"""
生成指定长度的主密钥
"""
key = ""
for i in range(key_len):
key += str(random.randint(0, 1))
return key
def lfsr(key, length):
"""
生成LFSR伪随机序列
"""
lfsr_seq = []
for i in range(length):
lfsr_seq.append(int(key[0]) ^ int(key[2]) ^ int(key[3]) ^ int(key[5]))
key = key[1:] + str(lfsr_seq[-1])
return lfsr_seq
def feistel_cipher(plain_text, key, rounds):
"""
Feistel结构分组密码算法实现
"""
block_size = 128
left_half = plain_text[:block_size//2]
right_half = plain_text[block_size//2:]
for i in range(rounds):
sub_key = key[i*block_size//rounds:i*block_size//rounds + block_size//rounds]
lfsr_seq = lfsr(sub_key, block_size//rounds)
temp = right_half
right_half = int(right_half, 2) ^ int("".join(map(str, lfsr_seq)), 2)
right_half = "{0:b}".format(right_half).zfill(block_size//2)
right_half = int(right_half, 2) ^ int(left_half, 2)
left_half = temp
cipher_text = left_half + right_half
return cipher_text
```
RSA公钥密码算法实现:
```python
from Crypto.Util.number import *
import random
def generate_key(key_len):
"""
生成RSA密钥对
"""
p = getPrime(key_len//2)
q = getPrime(key_len//2)
n = p * q
phi_n = (p - 1) * (q - 1)
while True:
e = random.randint(2, phi_n-1)
if GCD(e, phi_n) == 1:
break
d = inverse(e, phi_n)
return (n, e), (n, d)
def rsa_encrypt(plain_text, public_key):
"""
RSA加密算法实现
"""
n, e = public_key
plain_text_int = bytes_to_long(plain_text.encode())
cipher_text_int = pow(plain_text_int, e, n)
cipher_text = long_to_bytes(cipher_text_int).decode()
return cipher_text
def rsa_decrypt(cipher_text, private_key):
"""
RSA解密算法实现
"""
n, d = private_key
cipher_text_int = bytes_to_long(cipher_text.encode())
plain_text_int = pow(cipher_text_int, d, n)
plain_text = long_to_bytes(plain_text_int).decode()
return plain_text
```
数字信封实现:
```python
from Crypto.Cipher import AES
import os
def generate_session_key(key_len):
"""
生成会话密钥
"""
return os.urandom(key_len//8)
def encrypt_session_key(session_key, public_key):
"""
使用接收方公钥加密会话密钥
"""
cipher_key = rsa_encrypt(session_key, public_key)
return cipher_key
def decrypt_session_key(cipher_key, private_key):
"""
使用接收方私钥解密会话密钥
"""
session_key = rsa_decrypt(cipher_key, private_key)
return session_key
def ofb_mode_encrypt(plain_text, session_key):
"""
使用OFB模式加密明文文件
"""
iv = os.urandom(16)
cipher = AES.new(session_key, AES.MODE_OFB, iv)
cipher_text = cipher.encrypt(plain_text)
return iv + cipher_text
def ofb_mode_decrypt(cipher_text, session_key):
"""
使用OFB模式解密密文文件
"""
iv = cipher_text[:16]
cipher = AES.new(session_key, AES.MODE_OFB, iv)
plain_text = cipher.decrypt(cipher_text[16:])
return plain_text
def encrypt_file(file_path, public_key):
"""
加密文件
"""
with open(file_path, "rb") as f:
plain_text = f.read()
session_key = generate_session_key(256)
cipher_key = encrypt_session_key(session_key, public_key)
cipher_text = ofb_mode_encrypt(plain_text, session_key)
with open("c_text.txt", "wb") as f:
f.write(cipher_key.encode())
f.write(cipher_text)
def decrypt_file(file_path, private_key):
"""
解密文件
"""
with open(file_path, "rb") as f:
cipher_key = f.read(256)
cipher_text = f.read()
session_key = decrypt_session_key(cipher_key, private_key)
plain_text = ofb_mode_decrypt(cipher_text, session_key)
with open("p1_text.txt", "wb") as f:
f.write(plain_text)
```
注意:以上代码仅为示例,实际应用中需要考虑更多的安全问题和异常情况处理。
阅读全文