Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
D
DS-Project
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
DS-Project
Commits
f0a71ff8
Commit
f0a71ff8
authored
Nov 13, 2024
by
Almouhannad Hafez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement text_to_binary, binary_to_text
parent
fe864afd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
95 additions
and
5 deletions
+95
-5
project.ipynb
project.ipynb
+26
-4
s_des.py
s_des.py
+69
-1
No files found.
project.ipynb
View file @
f0a71ff8
...
...
@@ -31,15 +31,37 @@
"name": "stdout",
"output_type": "stream",
"text": [
"9\n",
"2\n"
"9\n"
]
}
],
"source": [
"sdes = SDES()\n",
"print(sdes.key_length)\n",
"print(sdes.rou)"
"print(sdes.key_length)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"168\n",
"Almouhannad.Hafez + 1\n",
"True\n"
]
}
],
"source": [
"original_text = \"Almouhannad.Hafez + 1\"\n",
"bin = sdes.text_to_binary(original_text)\n",
"print(len(bin))\n",
"text = sdes.binary_to_text(bin)\n",
"print(text)\n",
"print(text == original_text)\n"
]
}
],
...
...
s_des.py
View file @
f0a71ff8
...
...
@@ -11,6 +11,7 @@ class SDES:
key_length (int): Length of keys that will be used for encryption/decryption
"""
# SDES is a block-cipher algorithm, uses blocks of size 12 bits
# You can change this default value from here directly, and it'll applied to the whole code
self
.
block_size
=
12
# SDED uses a fixed-length key (8bits- key) for each iteration
...
...
@@ -72,4 +73,71 @@ class SDES:
"""
# Generate a random binary string by selecting '0' or '1' for each position
key
=
''
.
join
(
random
.
choice
(
'01'
)
for
_
in
range
(
self
.
key_length
))
return
key
\ No newline at end of file
return
key
def
text_to_binary
(
self
,
text
:
str
)
->
str
:
"""
Convert plain text to a binary string
with needed padding to be with length divisible by self.block_size
Parameters:
text (str): Text to be converted
Returns:
binary (str): Binary representation of the text
"""
# Type checking
if
not
isinstance
(
text
,
str
):
raise
TypeError
(
f
"'text' must be str, got {type(text).__name__}"
)
# Validation rules
if
len
(
text
)
==
0
:
raise
ValueError
(
"'text' must not be empty"
)
# Convert to binary
binary
=
''
.
join
(
format
(
ord
(
c
),
'08b'
)
for
c
in
text
)
# Handle padding to be with length divisible by self.block_size
binary_length
=
len
(
binary
)
# Calculate how much padding is needed to make it divisible by self.block_size
padding_needed
=
(
self
.
block_size
-
(
binary_length
%
self
.
block_size
))
%
self
.
block_size
# Add padding
if
padding_needed
>
0
:
binary
+=
'0'
*
padding_needed
return
binary
def
binary_to_text
(
self
,
binary
:
str
)
->
str
:
"""
Convert binary string to a plain text
Parameters:
binary (str): Binary data to be converted
Returns:
text (str): Plain text of the binary data
"""
# Type checking
if
not
isinstance
(
binary
,
str
):
raise
TypeError
(
f
"'binary' must be str, got {type(binary).__name__}"
)
# Validation rules
if
len
(
binary
)
==
0
:
raise
ValueError
(
"'binary' must not be empty"
)
if
len
(
binary
)
%
self
.
block_size
!=
0
:
raise
ValueError
(
f
"'binary' length must be a multiple of {self.block_size}"
)
if
not
all
(
c
in
'01'
for
c
in
binary
):
raise
ValueError
(
"'binary' must only contain '0' and '1'"
)
# Get chars (each char is 8 bit)
chars
=
[]
for
i
in
range
(
0
,
len
(
binary
),
8
):
chunk
=
binary
[
i
:
i
+
8
]
# Chack end of string
if
chunk
==
'00000000'
:
break
chars
.
append
(
chunk
)
# Convert to string plain text
text
=
''
.
join
(
chr
(
int
(
char
,
2
))
for
char
in
chars
)
return
text
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment