Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Smash_pass
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
1
Merge Requests
1
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
Smashpass
Smash_pass
Commits
94370b7e
Commit
94370b7e
authored
Dec 19, 2024
by
Lokmane OUADERHMAN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fart
parent
aec65bef
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
0 deletions
+67
-0
RSA_ENCRYPTION_2.0.py
Encryption Methods/RSA_ENCRYPTION_2.0.py
+67
-0
No files found.
Encryption Methods/RSA_ENCRYPTION_2.0.py
0 → 100644
View file @
94370b7e
import
math
# Helper functions
def
gcd
(
a
,
b
):
while
b
:
a
,
b
=
b
,
a
%
b
return
a
def
modinv
(
a
,
m
):
m0
,
x0
,
x1
=
m
,
0
,
1
while
a
>
1
:
q
=
a
//
m
m
,
a
=
a
%
m
,
m
x0
,
x1
=
x1
-
q
*
x0
,
x0
return
x1
+
m0
if
x1
<
0
else
x1
# Generate RSA Key Pair
def
generate_rsa_keys
():
p
=
61
# Example prime 1
q
=
53
# Example prime 2
n
=
p
*
q
phi
=
(
p
-
1
)
*
(
q
-
1
)
e
=
65537
# Common choice for e
while
gcd
(
e
,
phi
)
!=
1
:
e
+=
2
d
=
modinv
(
e
,
phi
)
public_key
=
(
e
,
n
)
private_key
=
(
d
,
n
)
return
private_key
,
public_key
# Encrypt data
def
encrypt_data
(
public_key
,
data
):
e
,
n
=
public_key
encrypted_data
=
b
""
.
join
(
pow
(
byte
,
e
,
n
)
.
to_bytes
((
n
.
bit_length
()
+
7
)
//
8
,
byteorder
=
"big"
)
for
byte
in
data
)
return
encrypted_data
# Decrypt data
def
decrypt_data
(
private_key
,
encrypted_data
):
d
,
n
=
private_key
key_size
=
(
n
.
bit_length
()
+
7
)
//
8
decrypted_data
=
bytes
(
pow
(
int
.
from_bytes
(
encrypted_data
[
i
:
i
+
key_size
],
byteorder
=
"big"
),
d
,
n
)
for
i
in
range
(
0
,
len
(
encrypted_data
),
key_size
)
)
return
decrypted_data
if
__name__
==
"__main__"
:
# Generate RSA keys
private_key
,
public_key
=
generate_rsa_keys
()
# Original data
original_data
=
b
"Hello RSA!"
print
(
f
"Original Data: {original_data}"
)
# Encrypt data
encrypted_data
=
encrypt_data
(
public_key
,
original_data
)
print
(
f
"Encrypted Data: {encrypted_data}"
)
# Decrypt data
decrypted_data
=
decrypt_data
(
private_key
,
encrypted_data
)
print
(
f
"Decrypted Data: {decrypted_data}"
)
# Verify decryption
assert
original_data
==
decrypted_data
,
"Decrypted data does not match original!"
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