Compare commits
3 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
4da9d7d6b0 | 4 years ago |
|
|
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()
|
||||
@ -0,0 +1,106 @@
|
||||
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 Sign(s, filepath):
|
||||
# 文件内容签名
|
||||
privkey = open('my-private.pem', "rb").read().decode("utf-8")
|
||||
key = RSA.importKey(privkey)
|
||||
data1 = open(filepath, "rb").read()
|
||||
h = SHA256.new(data1)
|
||||
signer = PKCS1_v1_5.new(key)
|
||||
signature1 = signer.sign(h)
|
||||
sig = base64.b64encode(signature1)
|
||||
s.send(sig)
|
||||
|
||||
|
||||
def save_file(content):
|
||||
f = open('Cipher.txt', 'w+')
|
||||
f.write(content)
|
||||
f.close()
|
||||
|
||||
|
||||
def send_message():
|
||||
while True:
|
||||
try:
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect(('127.0.0.1', 6666))
|
||||
except socket.error as msr:
|
||||
print(msr)
|
||||
print(sys.exit(1))
|
||||
|
||||
# 接收对方公钥
|
||||
print("#Receive the other party's public key...#")
|
||||
fileinfo_size = struct.calcsize('128sq')
|
||||
buf = s.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 = s.recv(1024)
|
||||
recvd_size += len(data)
|
||||
else:
|
||||
data = s.recv(1024)
|
||||
recvd_size = filesize
|
||||
fp.write(data)
|
||||
fp.close()
|
||||
print('#Receive successfully..................#')
|
||||
# END
|
||||
|
||||
# 发送己方公钥
|
||||
print("#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)
|
||||
s.send(head)
|
||||
fp = open('my-public.pem', 'rb')
|
||||
while True:
|
||||
data = fp.read(1024)
|
||||
if not data:
|
||||
break
|
||||
s.send(data)
|
||||
fp.close()
|
||||
# END
|
||||
|
||||
# 文件加密发送
|
||||
filepath = input('#Please input the file you want to send: ')
|
||||
with open(filepath, 'r') as f:
|
||||
ciphertext = f.readline()
|
||||
f.close()
|
||||
with open("new_my-public.pem") as f:
|
||||
key = f.read()
|
||||
rsakey = RSA.importKey(key)
|
||||
cipher = Cipher_pkcs1_v1_5.new(rsakey)
|
||||
cipher_text = base64.b64encode(cipher.encrypt(ciphertext.encode('utf-8')))
|
||||
print(cipher_text.decode('utf-8'))
|
||||
save_file(cipher_text.decode('utf-8'))
|
||||
|
||||
head = struct.pack(b'128sq', bytes(os.path.basename('Cipher.txt'), encoding='utf-8'),
|
||||
os.stat('Cipher.txt').st_size)
|
||||
s.send(head)
|
||||
|
||||
fp = open('Cipher.txt', 'rb')
|
||||
while True:
|
||||
data = fp.read(1024)
|
||||
if not data:
|
||||
print('{0} send over...\n'.format('Cipher.txt'))
|
||||
break
|
||||
s.send(data)
|
||||
fp.close()
|
||||
Sign(s, filepath)
|
||||
s.close()
|
||||
# END
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
send_message()
|
||||
Loading…
Reference in new issue