Compare commits
2 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
1558bc2e61 | 4 years ago |
|
|
bae6e5d19a | 4 years ago |
@ -0,0 +1,16 @@
|
|||||||
|
from Crypto import Random
|
||||||
|
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
|
||||||
|
random_generator = Random.new().read
|
||||||
|
rsa = RSA.generate(1024, random_generator)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
private_pem = rsa.exportKey()
|
||||||
|
with open("my-private.pem", "wb") as f:
|
||||||
|
f.write(private_pem)
|
||||||
|
|
||||||
|
public_pem = rsa.publickey().exportKey()
|
||||||
|
with open("my-public.pem", "wb") as f:
|
||||||
|
f.write(public_pem)
|
||||||
@ -0,0 +1,140 @@
|
|||||||
|
import base64
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
from Crypto.Signature import PKCS1_v1_5
|
||||||
|
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
|
||||||
|
from main import *
|
||||||
|
|
||||||
|
|
||||||
|
def save_file(content):
|
||||||
|
f = open('Plaintext.txt', 'w+')
|
||||||
|
f.write(content)
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
# RSA解密
|
||||||
|
def get_text():
|
||||||
|
with open('new_Cipher.txt', 'rb') as f:
|
||||||
|
plaintext = f.readline()
|
||||||
|
f.close()
|
||||||
|
plaintext = plaintext
|
||||||
|
with open("my-private.pem") as f:
|
||||||
|
key = f.read()
|
||||||
|
rsakey = RSA.importKey(key)
|
||||||
|
cipher = Cipher_pkcs1_v1_5.new(rsakey)
|
||||||
|
text = cipher.decrypt(base64.b64decode(plaintext), random_generator)
|
||||||
|
save_file(text.decode('utf-8'))
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
|
def receive_message():
|
||||||
|
try:
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
s.bind(('127.0.0.1', 6666))
|
||||||
|
s.listen(10)
|
||||||
|
except socket.error as msr:
|
||||||
|
print(msr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
print("#Wait for Connection.....................#")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
sock, addr = s.accept()
|
||||||
|
deal_image(sock, addr)
|
||||||
|
|
||||||
|
|
||||||
|
def verify(sock):
|
||||||
|
# 验证签名
|
||||||
|
signature = sock.recv(1024)
|
||||||
|
print(signature)
|
||||||
|
publickey = open('new_my-public.pem', "rb").read().decode('utf-8')
|
||||||
|
key = RSA.importKey(publickey)
|
||||||
|
data1 = open('Plaintext.txt', "rb").read()
|
||||||
|
h = SHA256.new(data1)
|
||||||
|
verifier = PKCS1_v1_5.new(key)
|
||||||
|
if verifier.verify(h, base64.b64decode(signature)):
|
||||||
|
print("#message is True#\n")
|
||||||
|
else:
|
||||||
|
print("#message is False#\n")
|
||||||
|
# END
|
||||||
|
|
||||||
|
|
||||||
|
def deal_image(sock, addr):
|
||||||
|
print("\nAccept connection from {0}".format(addr))
|
||||||
|
# 发送己方公钥
|
||||||
|
global new_filename
|
||||||
|
print("#Connect successfully, sending public key now........#")
|
||||||
|
head = struct.pack(b'128sq', bytes(os.path.basename('my-public.pem'), encoding='utf-8'),
|
||||||
|
os.stat('my-public.pem').st_size)
|
||||||
|
sock.send(head)
|
||||||
|
fp = open('my-public.pem', 'rb')
|
||||||
|
while True:
|
||||||
|
data = fp.read(1024)
|
||||||
|
if not data:
|
||||||
|
print('{0} send over...'.format('my-public.pem'))
|
||||||
|
break
|
||||||
|
sock.send(data)
|
||||||
|
fp.close()
|
||||||
|
# END
|
||||||
|
|
||||||
|
# 接收对方公钥
|
||||||
|
print("#Receive the other party's public key...#")
|
||||||
|
fileinfo_size = struct.calcsize('128sq')
|
||||||
|
buf = sock.recv(fileinfo_size)
|
||||||
|
if buf:
|
||||||
|
filename, filesize = struct.unpack('128sq', buf)
|
||||||
|
fn = filename.decode().strip('\x00')
|
||||||
|
new_filename = os.path.join('./', 'new_' + fn)
|
||||||
|
recvd_size = 0
|
||||||
|
fp = open(new_filename, 'wb')
|
||||||
|
while not recvd_size == filesize:
|
||||||
|
if filesize - recvd_size > 1024:
|
||||||
|
data = sock.recv(1024)
|
||||||
|
recvd_size += len(data)
|
||||||
|
else:
|
||||||
|
data = sock.recv(1024)
|
||||||
|
recvd_size = filesize
|
||||||
|
fp.write(data)
|
||||||
|
fp.close()
|
||||||
|
# END
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# 接收密文文件
|
||||||
|
fileinfo_size = struct.calcsize('128sq')
|
||||||
|
buf = sock.recv(fileinfo_size)
|
||||||
|
print('buf is ', buf)
|
||||||
|
if buf:
|
||||||
|
filename, filesize = struct.unpack('128sq', buf)
|
||||||
|
print('filename ,filesize is', filename.decode(), filesize)
|
||||||
|
fn = filename.decode().strip('\x00')
|
||||||
|
print('fn is ', fn)
|
||||||
|
new_filename = os.path.join('./', 'new_' + fn)
|
||||||
|
|
||||||
|
recvd_size = 0
|
||||||
|
fp = open(new_filename, 'wb')
|
||||||
|
while not recvd_size == filesize:
|
||||||
|
if filesize - recvd_size > 1024:
|
||||||
|
data = sock.recv(1024)
|
||||||
|
recvd_size += len(data)
|
||||||
|
else:
|
||||||
|
data = sock.recv(1024)
|
||||||
|
recvd_size = filesize
|
||||||
|
print('data is', data)
|
||||||
|
fp.write(data)
|
||||||
|
fp.close()
|
||||||
|
# END
|
||||||
|
|
||||||
|
get_text() # 解密
|
||||||
|
verify(sock)
|
||||||
|
|
||||||
|
sock.close()
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
receive_message()
|
||||||
Loading…
Reference in new issue