如何在上述基础上使用Socket编程来实现通信。
时间: 2024-02-18 15:04:29 浏览: 70
要使用Socket编程来实现通信,可以将上述代码分别放在两个程序中,一个程序作为Alice,另一个程序作为Bob,然后通过Socket来传输数据。下面是一个简单的示例代码,其中假设Alice和Bob分别运行在两台计算机上,它们的IP地址分别为192.168.1.100和192.168.1.101。
Alice端代码:
```python
import random
import hashlib
from Crypto.Cipher import DES
import socket
# Diffie-Hellman密码协商
def diffie_hellman(p, g, a, b):
A = pow(g, a, p)
B = pow(g, b, p)
K = pow(B, a, p)
return K
# 生成一个DES加密密钥
def generate_key():
key = ''
for i in range(8):
key += chr(random.randint(0, 255))
return key
# 加密
def encrypt(key, data):
cipher = DES.new(key, DES.MODE_ECB)
return cipher.encrypt(data)
# 解密
def decrypt(key, data):
cipher = DES.new(key, DES.MODE_ECB)
return cipher.decrypt(data)
# 计算MD5哈希值
def hash(data):
h = hashlib.md5()
h.update(data)
return h.hexdigest()
# 数字签名
def sign(private_key, data):
h = hash(data)
signature = pow(int(h, 16), private_key, p)
return signature
# 验证签名
def verify(public_key, signature, data):
h = hash(data)
left = pow(public_key, signature, p)
right = int(h, 16)
return left == right
# 生成素数
def generate_prime():
prime = 0
while True:
prime = random.randint(1000, 10000)
if is_prime(prime):
break
return prime
# 判断是否是素数
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# 主程序
if __name__ == '__main__':
# 生成素数p和本原根g
p = generate_prime()
g = random.randint(2, p-2)
print('p =', p)
print('g =', g)
# Alice和Bob分别生成私钥和公钥
a = random.randint(2, p-2)
b = random.randint(2, p-2)
A = pow(g, a, p)
B = pow(g, b, p)
# Alice和Bob运行Diffie-Hellman密码协商协议生成共享密钥K
K = diffie_hellman(p, g, a, b)
print('K =', K)
# Alice将自己的公钥发送给Bob
print('Alice -> Bob: A =', A)
# 连接Bob的计算机
host = '192.168.1.101'
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# Bob使用Alice的公钥生成对称密钥K1
B = s.recv(1024)
B = int(B.decode())
K1 = pow(B, a, p)
print('K1 =', K1)
# Alice将秘密信息M加密为C1,并使用MD5算法生成哈希值H1
M = 'Hello, Bob!'
key = generate_key()
C1 = encrypt(key, M.encode())
H1 = hash(M.encode())
# Alice使用自己的私钥对哈希值H1进行数字签名,生成签名值S1
d = random.randint(2, p-2)
private_key = d
public_key = pow(g, d, p)
S1 = sign(private_key, H1.encode())
# Alice将加密后的信息C1、哈希值H1和签名值S1发送给Bob
data = str(C1) + '|' + str(H1) + '|' + str(S1)
s.send(data.encode())
# 关闭连接
s.close()
```
Bob端代码:
```python
import random
import hashlib
from Crypto.Cipher import DES
import socket
# Diffie-Hellman密码协商
def diffie_hellman(p, g, a, b):
A = pow(g, a, p)
B = pow(g, b, p)
K = pow(A, b, p)
return K
# 生成一个DES加密密钥
def generate_key():
key = ''
for i in range(8):
key += chr(random.randint(0, 255))
return key
# 加密
def encrypt(key, data):
cipher = DES.new(key, DES.MODE_ECB)
return cipher.encrypt(data)
# 解密
def decrypt(key, data):
cipher = DES.new(key, DES.MODE_ECB)
return cipher.decrypt(data)
# 计算MD5哈希值
def hash(data):
h = hashlib.md5()
h.update(data)
return h.hexdigest()
# 数字签名
def sign(private_key, data):
h = hash(data)
signature = pow(int(h, 16), private_key, p)
return signature
# 验证签名
def verify(public_key, signature, data):
h = hash(data)
left = pow(public_key, signature, p)
right = int(h, 16)
return left == right
# 生成素数
def generate_prime():
prime = 0
while True:
prime = random.randint(1000, 10000)
if is_prime(prime):
break
return prime
# 判断是否是素数
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
# 主程序
if __name__ == '__main__':
# 生成素数p和本原根g
p = generate_prime()
g = random.randint(2, p-2)
print('p =', p)
print('g =', g)
# Alice和Bob分别生成私钥和公钥
a = random.randint(2, p-2)
b = random.randint(2, p-2)
A = pow(g, a, p)
B = pow(g, b, p)
# Bob将自己的公钥发送给Alice
host = '192.168.1.100'
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
conn.send(str(B).encode())
# Alice使用Bob的公钥生成对称密钥K1
A = conn.recv(1024)
A = int(A.decode())
K1 = pow(A, b, p)
print('K1 =', K1)
# Bob接收加密后的信息C1、哈希值H1和签名值S1,并进行解密和验证
data = conn.recv(1024)
data = data.decode()
C1, H1, S1 = data.split('|')
C1 = eval(C1)
H1 = eval(H1)
S1 = int(S1)
# Bob使用Alice的公钥验证签名值S1,并使用对称密钥K1解密C1得到M1
if verify(A, S1, H1.encode()):
M1 = decrypt(generate_key(), C1).decode()
print('Bob received:', M1)
else:
print('Signature verification failed!')
# 关闭连接
conn.close()
s.close()
```
注意,这个代码只是简单的示例,还需要加入错误处理等功能。
阅读全文