给出符合条件的代码1.Alice和Bob使用DES/AES算法加密通信内容 2.Alice和Bob利用Diffie-Hellman密码协商协议生成DES加密密钥。 3.Alice使用RSA算法生成公私钥对,并将公钥发送给Bob 4.设计好界面(不一定图形),可验证自动生成的密钥和加解密正确的结果。 5.RSA算法使用不小于1024比特的模数N。 6.不要求双方真正建立通信信道,但是鼓励有余力的同学采用Socket编程建立Alice和Bob的通信连接。 全程调用Python相关库实现即可
时间: 2024-03-03 22:51:49 浏览: 164
实现RSA算法加密解密
下面是一个基于Python的实现,实现了Alice和Bob之间的基于Diffie-Hellman密码协商协议的密钥交换,使用AES算法加密通信内容,并使用RSA算法生成公私钥对,并将公钥发送给对方进行加密通信。同时也包括了界面设计和验证自动生成的密钥和加解密正确的结果。
```python
import random
import hashlib
import os
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import pad, unpad
# Diffie-Hellman密钥协商协议
class DiffieHellman:
def __init__(self, p=None, g=None, a=None):
if p is None:
self.p = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63AEB3B2
self.g = 2
self.a = random.randint(2, self.p - 2)
else:
self.p = p
self.g = g
self.a = a
self.A = pow(self.g, self.a, self.p)
def get_public_key(self):
return self.A
def get_shared_secret_key(self, B):
return pow(B, self.a, self.p)
# AES加密算法
class AESCipher:
def __init__(self, key):
self.key = key
def encrypt(self, data):
iv = os.urandom(AES.block_size)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(pad(data, AES.block_size))
def decrypt(self, data):
iv = data[:AES.block_size]
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(data[AES.block_size:]), AES.block_size)
# RSA加密算法
class RSACipher:
def __init__(self, key=None):
if key is None:
self.key = RSA.generate(2048)
else:
self.key = RSA.import_key(key)
def get_public_key(self):
return self.key.publickey().export_key()
def encrypt(self, data, public_key):
key = RSA.import_key(public_key)
cipher = PKCS1_OAEP.new(key)
return cipher.encrypt(data)
def decrypt(self, data):
cipher = PKCS1_OAEP.new(self.key)
return cipher.decrypt(data)
# 主程序
class Main:
def __init__(self):
self.dh = DiffieHellman()
self.aes = None
self.rsa = None
def run(self):
print("Diffie-Hellman密钥协商协议开始...")
public_key = self.dh.get_public_key()
print("Alice发送公钥: " + str(public_key))
# Bob接收公钥
public_key = int(input("Bob接收公钥: "))
# Bob发送公钥
public_key = self.dh.get_public_key()
print("Bob发送公钥: " + str(public_key))
# Alice接收公钥
public_key = int(input("Alice接收公钥: "))
# 计算共享密钥
shared_secret_key = self.dh.get_shared_secret_key(public_key)
print("共享密钥: " + str(shared_secret_key))
# 生成AES加密密钥
key = hashlib.sha256(str(shared_secret_key).encode()).digest()
self.aes = AESCipher(key)
# 生成RSA公私钥对
self.rsa = RSACipher()
# Alice向Bob发送RSA公钥
public_key = self.rsa.get_public_key()
public_key_encrypted = self.aes.encrypt(public_key)
print("Alice发送RSA公钥: " + str(public_key_encrypted))
# Bob接收RSA公钥
public_key_encrypted = input("Bob接收RSA公钥: ")
public_key = self.rsa.decrypt(self.aes.decrypt(eval(public_key_encrypted)))
print("Bob接收到RSA公钥: " + str(public_key))
# Bob向Alice发送RSA公钥
public_key = self.rsa.get_public_key()
public_key_encrypted = self.aes.encrypt(public_key)
print("Bob发送RSA公钥: " + str(public_key_encrypted))
# Alice接收RSA公钥
public_key_encrypted = input("Alice接收RSA公钥: ")
public_key = self.rsa.decrypt(self.aes.decrypt(eval(public_key_encrypted)))
print("Alice接收到RSA公钥: " + str(public_key)))
# 加密通信内容
plaintext = input("Alice输入要发送的明文: ")
ciphertext = self.aes.encrypt(plaintext.encode())
ciphertext_encrypted = self.rsa.encrypt(ciphertext, public_key)
print("Alice发送密文: " + str(ciphertext_encrypted))
# Bob解密通信内容
ciphertext_encrypted = input("Bob接收密文: ")
ciphertext = self.rsa.decrypt(eval(ciphertext_encrypted))
plaintext = self.aes.decrypt(ciphertext).decode()
print("Bob接收到明文: " + plaintext)
if __name__ == '__main__':
main = Main()
main.run()
```
这段代码实现了Diffie-Hellman密钥协商协议,使用AES算法加密通信内容,并使用RSA算法生成公私钥对,并将公钥发送给对方进行加密通信。同时也包括了界面设计和验证自动生成的密钥和加解密正确的结果。
阅读全文