Alice和Bob使用DES/AES算法加密通信内容 2. Alice和Bob利用Diffie-Hellman密码协商协议生成DES加密密钥k_(AB) 。 3. Alice使用RSA算法生成公私钥对,并将公钥发送给Bob 4. 设计好界面(不一定图形),可验证自动生成的密钥和加解密正确的结果。 5. RSA算法使用不小于1024比特的模数N。 6. 不要求双方真正建立通信信道,但是鼓励有余力的同学采用Socket编程建立Alice和Bob的通信连接。 7. 全程调用Python相关库实现即可(有余力的同学可以尝试自己编程实现密码算法。素数生成过程中可使用标准库,如gmp、BigInteger等)。python代码实现
时间: 2023-12-03 21:46:15 浏览: 98
以下是一个基本的实现,需要注意的是,为了简化代码,我们使用了Python中的cryptography库来实现加密算法和Diffie-Hellman密钥协商。另外,由于RSA算法的实现相对较为复杂,我们选择使用了PyCryptodome库来实现。需要在终端中安装这两个库才能正常运行代码。
```python
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives.ciphers import modes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hmac
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import dh
import os
# Diffie-Hellman密钥协商
def dh_key_exchange():
parameters = dh.generate_parameters(generator=2, key_size=512)
alice_private_key = parameters.generate_private_key()
bob_private_key = parameters.generate_private_key()
alice_public_key = alice_private_key.public_key().public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
bob_public_key = bob_private_key.public_key().public_bytes(
encoding=serialization.Encoding.DER,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
alice_shared_key = alice_private_key.exchange(
serialization.load_der_public_key(bob_public_key)
)
bob_shared_key = bob_private_key.exchange(
serialization.load_der_public_key(alice_public_key)
)
# 生成DES加密密钥
salt = os.urandom(16)
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=16,
salt=salt,
iterations=100000,
)
key = kdf.derive(alice_shared_key)
return key
# 加密函数
def encrypt(key, plaintext):
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
encryptor = cipher.encryptor()
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
return (iv, ciphertext)
# 解密函数
def decrypt(key, iv, ciphertext):
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext) + decryptor.finalize()
return plaintext
# RSA加密函数
def rsa_encrypt(public_key, plaintext):
ciphertext = public_key.encrypt(
plaintext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return ciphertext
# RSA解密函数
def rsa_decrypt(private_key, ciphertext):
plaintext = private_key.decrypt(
ciphertext,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return plaintext
# HMAC函数
def hmac_sha256(key, message):
h = hmac.HMAC(key, hashes.SHA256())
h.update(message)
return h.finalize()
# 生成RSA公私钥对
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048
)
public_key = private_key.public_key()
# Diffie-Hellman密钥协商
key = dh_key_exchange()
# 需要加密的明文
plaintext = b"Hello, Bob!"
# 使用DES加密
iv, ciphertext = encrypt(key, plaintext)
# 使用RSA加密密钥
encrypted_key = rsa_encrypt(public_key, key)
# 计算HMAC值
hmac_value = hmac_sha256(key, ciphertext)
# 将加密后的结果发送给Bob
# 假设Bob已经收到了消息并解密出了加密密钥和密文
# 现在Bob需要使用私钥解密加密密钥并使用该密钥解密明文
# 同时还需要重新计算HMAC值并与接收到的HMAC值进行比较
decrypted_key = private_key.decrypt(
encrypted_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
decrypted_plaintext = decrypt(decrypted_key, iv, ciphertext)
# 计算HMAC值
new_hmac_value = hmac_sha256(decrypted_key, decrypted_plaintext)
# 比较HMAC值
if hmac.compare_digest(hmac_value, new_hmac_value):
print("HMAC值匹配,解密成功!")
print("明文为:", decrypted_plaintext)
else:
print("HMAC值不匹配,解密失败!")
```
需要注意的是,该实现仅仅是一个基本的演示,实际应用中还需要考虑其他因素,如密钥管理、密钥更新、消息认证等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)