Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_121 - Travail Pratique 002 - RSA
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_121 - Travail Pratique 002 - RSA
Commits
93280e3d
Commit
93280e3d
authored
3 years ago
by
dario.genga
Browse files
Options
Downloads
Plain Diff
Merge branch 'master' of
ssh://ssh.hesge.ch:10572/florian.burgener/math-tp-002
parents
ef3a396c
29578829
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
rsa.py
+47
-91
47 additions, 91 deletions
rsa.py
énoncé/RSA_Groupe_13.txt
+0
-0
0 additions, 0 deletions
énoncé/RSA_Groupe_13.txt
énoncé/TP2-RSA.pdf
+0
-0
0 additions, 0 deletions
énoncé/TP2-RSA.pdf
with
47 additions
and
91 deletions
rsa.py
+
47
−
91
View file @
93280e3d
"""
Project : T
P numéro 2 :
RSA
Lesson
: I
T
121 – 2021-2022
Authors : Florian BURGENER, Gawen ACKERMANN, Quentin FASLER,
Dario GENGA
Project : T
ravail Pratique
RSA
Course
: I
SC_
121 – 2021-2022
Authors :
Dario GENGA,
Florian BURGENER, Gawen ACKERMANN, Quentin FASLER,
Date : 2021-2022
"""
import
math
def
get_bezout_coefficients
(
a
,
b
):
"""
Find the Bézout coefficients
o
f a and b.
"""
Find the Bézout coefficients f
or the numbers
a and b.
Args:
a (int): The number a
b (int): The number b.
Returns:
tuple: Bézout coefficients.
tuple:
The
Bézout coefficients.
"""
r
=
[
a
,
b
]
x
=
[
1
,
0
]
...
...
@@ -24,24 +25,26 @@ def get_bezout_coefficients(a, b):
while
r
[
i
]
>
0
:
i
+=
1
r
.
append
(
r
[
i
-
2
]
%
r
[
i
-
1
])
q
.
append
(
int
(
r
[
i
-
2
]
/
r
[
i
-
1
]))
if
r
[
i
]
>
0
:
x
.
append
(
x
[
i
-
2
]
-
q
[
i
]
*
x
[
i
-
1
])
y
.
append
(
y
[
i
-
2
]
-
q
[
i
]
*
y
[
i
-
1
])
return
x
[
-
1
],
y
[
-
1
]
def
modular_inverse
(
a
,
n
):
"""
C
ompu
te the modular inverse of a number a modulo n.
"""
C
alcula
te
s
the modular inverse of a number a modulo n.
Args:
a (int): The number to reverse.
a (int): The number to
be
reverse
d
.
n (int): The modulo.
Returns:
int: The
re
ver
s
ed number.
int: The
in
ver
t
ed number.
"""
coefficients
=
get_bezout_coefficients
(
a
,
n
)
...
...
@@ -50,119 +53,72 @@ def modular_inverse(a, n):
return
None
def
get_q_p
(
n
):
"""
Get the value of q and p from n by
bruteforce.
def
break_encryption
(
n
):
"""
Breaks RSA encryption using the
brute
force
technique
.
Args:
n (int):
One of the two value of the public key
.
n (int):
The component of the public key which is the product of p and q
.
Returns:
tuple: The
two value that represent n
.
tuple: The
prime numbers p and q
.
"""
p
=
2
while
True
:
if
n
%
p
==
0
:
q
=
n
//
p
return
(
p
,
q
)
p
+=
1
range_low
=
3
range_high
=
int
(
math
.
ceil
(
math
.
sqrt
(
n
)))
for
x
in
[
2
]
+
list
(
range
(
range_low
,
range_high
,
2
)):
if
n
%
x
==
0
:
return
(
x
,
n
//
x
)
return
None
def
modular_pow
(
base
,
exponent
,
modulus
):
"""
Compute the modular
power
.
"""
Compute
s
the modular
exponentiation
.
Args:
base (int):
The value of a part of the message to be decoded
.
exponent (int):
The private key
.
modulus (int):
One of the two value of the public key
.
base (int):
Power base
.
exponent (int):
Power exponent
.
modulus (int):
The modulos
.
Returns:
int: The
int value of the value, ready to be decoded to utf-8
.
int: The
result of the exponentiation
.
"""
if
modulus
==
1
:
return
0
result
=
1
base
%=
modulus
while
exponent
>
0
:
if
exponent
%
2
==
1
:
result
=
(
result
*
base
)
%
modulus
exponent
=
exponent
>>
1
base
=
(
base
*
base
)
%
modulus
return
result
def
main
():
e
=
5249
n
=
1653973759
messages
=
(
1511395078
,
260436590
,
1630654276
,
1190458520
,
790492067
,
515550941
,
297140366
,
755589582
,
647075331
,
1191707844
,
901889430
,
660956124
,
1500654109
,
984322720
,
1275630738
,
1244853107
,
1445928913
,
1312523810
,
265093060
,
933013993
,
1375592761
,
195866064
,
534502441
,
928270408
,
166404031
,
621272622
,
1304987439
,
905393335
,
55120151
,
772595721
,
506609577
,
1172751778
,
162439707
,
233959833
,
1468937795
,
1358701120
,
901889430
,
495995733
,
1524090698
,
1043509086
,
934992314
,
1545639379
,
1061595897
,
1348452679
,
1135067876
,
905393335
,
621272622
,
55120151
,
233959833
,
1220119699
,
708711266
,
517797467
,
195866064
,
1579814353
,
412378626
,
498875436
,
445485200
,
7656659
)
p
,
q
=
get_q_p
(
n
)
encrypted_data
=
(
1511395078
,
260436590
,
1630654276
,
1190458520
,
790492067
,
515550941
,
297140366
,
755589582
,
647075331
,
1191707844
,
901889430
,
660956124
,
1500654109
,
984322720
,
1275630738
,
1244853107
,
1445928913
,
1312523810
,
265093060
,
933013993
,
1375592761
,
195866064
,
534502441
,
928270408
,
166404031
,
621272622
,
1304987439
,
905393335
,
55120151
,
772595721
,
506609577
,
1172751778
,
162439707
,
233959833
,
1468937795
,
1358701120
,
901889430
,
495995733
,
1524090698
,
1043509086
,
934992314
,
1545639379
,
1061595897
,
1348452679
,
1135067876
,
905393335
,
621272622
,
55120151
,
233959833
,
1220119699
,
708711266
,
517797467
,
195866064
,
1579814353
,
412378626
,
498875436
,
445485200
,
7656659
)
p
,
q
=
break_encryption
(
n
)
# Calculation of the decryption key.
d
=
modular_inverse
(
e
,
(
p
-
1
)
*
(
q
-
1
))
result
=
""
for
x
in
messages
:
tmp
=
modular_pow
(
x
,
d
,
n
)
mBytes
=
tmp
.
to_bytes
(
tmp
.
bit_length
(),
byteorder
=
"
little
"
)
result
+=
mBytes
.
decode
(
"
utf-8
"
)
decrypted_data
=
[]
for
x
in
encrypted_data
:
decrypted_data
.
append
(
modular_pow
(
x
,
d
,
n
))
decoded_data
=
""
for
x
in
decrypted_data
:
decoded_data
+=
x
.
to_bytes
((
x
.
bit_length
()
+
7
)
//
8
,
"
little
"
).
decode
(
"
utf-8
"
)
print
(
result
)
print
(
decoded_data
)
if
__name__
==
"
__main__
"
:
...
...
This diff is collapsed.
Click to expand it.
annexe /
Group_13
_Enonce
.txt
→
énoncé/RSA_
Group
e
_13.txt
+
0
−
0
View file @
93280e3d
File moved
This diff is collapsed.
Click to expand it.
annexe /enonce_tp_
RSA.pdf
→
énoncé/TP2-
RSA.pdf
+
0
−
0
View file @
93280e3d
File moved
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