Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SSE-IoT-Encryption
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dominik Fuhrmann
SSE-IoT-Encryption
Commits
84642bb3
Commit
84642bb3
authored
6 months ago
by
Dominik Fuhrmann
Browse files
Options
Downloads
Patches
Plain Diff
hex instead of binaries
parent
9f33c832
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
communication/client.py
+15
-9
15 additions, 9 deletions
communication/client.py
communication/server.py
+9
-8
9 additions, 8 deletions
communication/server.py
with
24 additions
and
17 deletions
communication/client.py
+
15
−
9
View file @
84642bb3
...
...
@@ -6,6 +6,7 @@ from Crypto.Cipher import DES, Blowfish, ARC4, PKCS1_OAEP
from
Crypto.PublicKey
import
RSA
from
Crypto.Util.Padding
import
pad
from
Crypto.Random
import
get_random_bytes
import
base64
import
sys
import
os
sys
.
path
.
append
(
os
.
path
.
abspath
(
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
'
..
'
,
'
setup
'
)))
...
...
@@ -21,7 +22,7 @@ def load_public_key():
def
encrypt_key_with_rsa
(
public_key
,
key
):
cipher
=
PKCS1_OAEP
.
new
(
public_key
)
encrypted_key
=
cipher
.
encrypt
(
key
)
return
encrypted_key
# return as raw bytes, not base64 en
code
d
return
base64
.
b64encode
(
encrypted_key
).
de
code
()
# Encrypt entered user message according to selected algorithm and key length
def
encrypt_message
(
algorithm
,
key
,
message
):
...
...
@@ -30,17 +31,23 @@ def encrypt_message(algorithm, key, message):
if
algorithm
==
'
DES
'
:
cipher
=
DES
.
new
(
key
,
DES
.
MODE_CBC
)
ciphertext
=
cipher
.
encrypt
(
pad
(
message
.
encode
(),
DES
.
block_size
))
return
cipher
.
iv
+
ciphertext
# return raw bytes (iv + ciphertext)
encrypted_message
=
(
cipher
.
iv
+
ciphertext
).
hex
()
logging
.
info
(
"
DES encryption successful.
"
)
return
encrypted_message
elif
algorithm
==
'
Blowfish
'
:
cipher
=
Blowfish
.
new
(
key
,
Blowfish
.
MODE_CBC
)
ciphertext
=
cipher
.
encrypt
(
pad
(
message
.
encode
(),
Blowfish
.
block_size
))
return
cipher
.
iv
+
ciphertext
# return raw bytes (iv + ciphertext)
encrypted_message
=
(
cipher
.
iv
+
ciphertext
).
hex
()
logging
.
info
(
"
Blowfish encryption successful.
"
)
return
encrypted_message
elif
algorithm
==
'
RC4
'
:
cipher
=
ARC4
.
new
(
key
)
ciphertext
=
cipher
.
encrypt
(
message
.
encode
())
return
ciphertext
# return raw bytes of the encrypted message
encrypted_message
=
ciphertext
.
hex
()
logging
.
info
(
"
RC4 encryption successful.
"
)
return
encrypted_message
else
:
logging
.
error
(
"
Unsupported encryption algorithm.
"
)
...
...
@@ -48,12 +55,12 @@ def encrypt_message(algorithm, key, message):
# Send data to server
def
send_message_to_server
(
host
,
port
,
algorithm
,
key
,
key_length
,
encrypted_message
,
encrypted_key
):
# Configure data to send
(we no longer encode the encrypted data in base64)
# Configure data to send
data
=
{
'
algorithm
'
:
algorithm
,
'
key_length
'
:
key_length
,
'
encrypted_key
'
:
encrypted_key
,
# raw bytes
'
encrypted_message
'
:
encrypted_message
# raw bytes
'
encrypted_key
'
:
encrypted_key
,
'
encrypted_message
'
:
encrypted_message
}
# Start connection to server and send data
...
...
@@ -62,7 +69,6 @@ def send_message_to_server(host, port, algorithm, key, key_length, encrypted_mes
try
:
s
.
connect
((
host
,
port
))
logging
.
info
(
f
"
Connected to server at
{
host
}
:
{
port
}
"
)
# Send the raw data (no base64 encoding)
s
.
sendall
(
json
.
dumps
(
data
).
encode
())
logging
.
info
(
"
Data sent to server.
"
)
except
Exception
as
e
:
...
...
@@ -156,4 +162,4 @@ def main():
break
if
__name__
==
'
__main__
'
:
main
()
main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
communication/server.py
+
9
−
8
View file @
84642bb3
...
...
@@ -27,8 +27,9 @@ def decrypt_key_with_rsa(private_key, encrypted_key):
def
decrypt_message
(
algorithm
,
key
,
encrypted_message
):
logging
.
info
(
f
"
Starting decryption with algorithm:
{
algorithm
}
"
)
# No base64 decoding here since we're working with raw bytes
logging
.
info
(
f
"
Encrypted message (raw bytes):
{
encrypted_message
.
hex
()
}
"
)
# Transform received message from hex to binary
encrypted_message
=
bytes
.
fromhex
(
encrypted_message
)
logging
.
info
(
f
"
Encrypted message:
{
encrypted_message
.
hex
()
}
"
)
if
algorithm
==
'
DES
'
:
iv
=
encrypted_message
[:
8
]
...
...
@@ -61,17 +62,17 @@ def decrypt_message(algorithm, key, encrypted_message):
def
handle_client
(
conn
,
addr
,
private_key
):
logging
.
info
(
f
"
Handling client
{
addr
}
"
)
# Receive data from client
data
=
conn
.
recv
(
1024
)
# Receive
up to 1024 Bytes
data from client
data
=
conn
.
recv
(
1024
)
.
decode
()
logging
.
info
(
f
"
Received data from client
{
addr
}
.
"
)
# Manage received JSON data and extract information
try
:
data
=
json
.
loads
(
data
.
decode
()
)
data
=
json
.
loads
(
data
)
algorithm
=
data
[
'
algorithm
'
]
key_length
=
data
[
'
key_length
'
]
encrypted_key
=
data
[
'
encrypted_key
'
]
#
Now it's raw bytes, no base64 encoding
encrypted_message
=
data
[
'
encrypted_message
'
]
# Raw bytes
encrypted_key
=
base64
.
b64decode
(
data
[
'
encrypted_key
'
]
)
#
Assuming encrypted_key is still in Base64
encrypted_message
=
data
[
'
encrypted_message
'
]
logging
.
info
(
f
"
Algorithm:
{
algorithm
}
, Key Length:
{
key_length
}
"
)
except
json
.
JSONDecodeError
as
e
:
logging
.
error
(
f
"
Error decoding data:
{
e
}
"
)
...
...
@@ -131,4 +132,4 @@ def main():
continue
if
__name__
==
'
__main__
'
:
main
()
main
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment