Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c2911f

Browse files
Submission1
0 parents commit 9c2911f

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎sslClient.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import socket
2+
import ssl
3+
import sys
4+
from dataFile import *
5+
from Crypto.Cipher import AES
6+
from Crypto.Hash import SHA256
7+
from Crypto import Random
8+
9+
context = ssl.create_default_context()
10+
11+
conn = context.wrap_socket(socket.socket(socket.AF_INET),server_hostname='')
12+
conn.connect(('', 9500))
13+
cert = conn.getpeercert()
14+
15+
def encrypt(key, filename):
16+
chunksize = 64*1024
17+
outputFile = "(encrypted)"+filename
18+
filesize = str(os.path.getsize(filename)).zfill(16)
19+
IV = Random.new().read(16)
20+
21+
encryptor = AES.new(key, AES.MODE_CBC, IV)
22+
23+
with open(filename, 'rb') as infile:
24+
with open(outputFile, 'wb') as outfile:
25+
outfile.write(filesize.encode('utf-8'))
26+
outfile.write(IV)
27+
28+
while True:
29+
chunk = infile.read(chunksize)
30+
if len(chunk) == 0:
31+
break
32+
elif len(chunk) % 16 != 0:
33+
chunk +=b'' * (16- (len(chunk) %16))
34+
outfile.write(encryptor.encrypt(chunk))
35+
return outputFile
36+
37+
def getKey(password):
38+
hasher = SHA256.new(password.encode('utf-8'))
39+
return hasher.digest()
40+
41+
42+
conn.sendall(outputFile)

‎sslServer.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import socket
2+
import ssl
3+
import sys
4+
from Crypto.Cipher import AES
5+
from Crypto.Hash import SHA256
6+
from Crypto import Random
7+
8+
9+
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
10+
context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
11+
12+
bindsocket = socket.socket()
13+
bindsocket.bind(('', 9500))
14+
bindsocket.listen(5)
15+
16+
while True:
17+
newsocket, fromaddr = bindsocket.accept()
18+
connstream = context.wrap_socket(newsocket, server_side=True)
19+
try:
20+
decrypt(connstream)
21+
finally:
22+
connstream.shutdown(socket.SHUT_RDWR)
23+
connstream.close()
24+
25+
def decrypt(key,filename,connstream):
26+
chunksize = 64*1024
27+
outputFile = filename[11:]
28+
data = connstream.recv(4069)
29+
30+
with open(filename, 'rb') as infile:
31+
filesize = int(infile.read(16))
32+
IV = infile.read(16)
33+
34+
decryptor = AES.new(key, AES.MODE_CBC, IV)
35+
with open(outputFile, 'wb') as outfile:
36+
while True:
37+
chunk = infile.read(chunksize)
38+
if len(chunk) == 0:
39+
break
40+
outfile.write(decryptor.decrypt(chunk))
41+
outfile.truncate(filesize)
42+
return outfile
43+
44+
def getKey(password):
45+
hasher = SHA256.new(password.encode('utf-8'))
46+
return hasher.digest()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /