Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TP - 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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
valentin.bernillo
TP - RSA
Commits
04ee9293
Commit
04ee9293
authored
3 years ago
by
valentin.bernillo
Browse files
Options
Downloads
Patches
Plain Diff
Ajout de fonctions et modification du main
parent
50081d13
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
code/functions.py
+29
-2
29 additions, 2 deletions
code/functions.py
code/main.py
+43
-5
43 additions, 5 deletions
code/main.py
with
72 additions
and
7 deletions
code/functions.py
+
29
−
2
View file @
04ee9293
...
@@ -12,6 +12,8 @@
...
@@ -12,6 +12,8 @@
"""
L
'
objectif de ce Travail Pratique est de déchiffrer un message codé via l
'
algorythme RSA
"""
"""
L
'
objectif de ce Travail Pratique est de déchiffrer un message codé via l
'
algorythme RSA
"""
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Imports
# Imports
import
math
import
time
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Fonction qui calcule le PGCD d'un entier a et d'un entier b selon la méthode d'Euclide
# Fonction qui calcule le PGCD d'un entier a et d'un entier b selon la méthode d'Euclide
...
@@ -44,7 +46,7 @@ def PGCD(a, b):
...
@@ -44,7 +46,7 @@ def PGCD(a, b):
# Fonction qui calcule les coéfficient de Bézout ainsi que le PGCD d'un entier a et d'un entier b selon la méthode d'Euclide étendue
# Fonction qui calcule les coéfficient de Bézout ainsi que le PGCD d'un entier a et d'un entier b selon la méthode d'Euclide étendue
# Résultat sous la forme 𝑃𝐺𝐶𝐷 𝑎, 𝑏 = 𝑥 × 𝑎 + 𝑦 × 𝑏
# Résultat sous la forme 𝑃𝐺𝐶𝐷 𝑎, 𝑏 = 𝑥 × 𝑎 + 𝑦 × 𝑏
def
EuclideEtendu
(
a
,
b
):
def
Euclide
_
Etendu
(
a
,
b
):
if
(
a
!=
b
):
if
(
a
!=
b
):
# Si a est plus petit que b, échange leurs valeurs (a = b, b = a)
# Si a est plus petit que b, échange leurs valeurs (a = b, b = a)
if
(
a
<
b
):
if
(
a
<
b
):
...
@@ -85,6 +87,7 @@ def EuclideEtendu(a, b):
...
@@ -85,6 +87,7 @@ def EuclideEtendu(a, b):
pgcd
=
r
[
i
-
1
]
pgcd
=
r
[
i
-
1
]
X
=
x
[
i
-
1
]
X
=
x
[
i
-
1
]
Y
=
y
[
i
-
1
]
Y
=
y
[
i
-
1
]
print
(
"
%d x %d + %d x %d = %d
"
%
(
a
,
X
,
b
,
Y
,
pgcd
))
if
(
X
*
a
+
Y
*
b
!=
pgcd
):
if
(
X
*
a
+
Y
*
b
!=
pgcd
):
print
(
"
Une erreur s
'
est produite
"
)
print
(
"
Une erreur s
'
est produite
"
)
...
@@ -94,7 +97,7 @@ def EuclideEtendu(a, b):
...
@@ -94,7 +97,7 @@ def EuclideEtendu(a, b):
print
(
"
Erreur, a = b
"
)
print
(
"
Erreur, a = b
"
)
# Fonction calculant le résultat du calcul (a ^ X) mod n, avec pour objectif d'utiliser une valeur X très grande, grâce à l'exponentiation rapide
# Fonction calculant le résultat du calcul (a ^ X) mod n, avec pour objectif d'utiliser une valeur X très grande, grâce à l'exponentiation rapide
def
ExponentiationRapide
(
a
,
X
,
n
):
def
Exponentiation
_
Rapide
(
a
,
X
,
n
):
if
(
a
==
0
):
if
(
a
==
0
):
r
=
0
r
=
0
return
r
return
r
...
@@ -119,3 +122,27 @@ def ExponentiationRapide(a, X, n):
...
@@ -119,3 +122,27 @@ def ExponentiationRapide(a, X, n):
e
=
e
/
2
e
=
e
/
2
return
r
return
r
# Fonction servant à calculer la valeur de p
# Retourne le nombre d'itérations ainsi que la valeur de p
def
Calculer_P
(
n
):
nb
=
math
.
ceil
(
math
.
sqrt
(
n
))
nbIterations
=
0
for
p
in
range
(
nb
,
2
,
-
2
):
nbIterations
+=
1
if
n
%
p
==
0
:
return
nbIterations
,
p
# Fonction calculant la valeur de Phi, en fonction de p et q
def
Calculer_Phi
(
p
,
q
):
return
((
p
-
1
)
*
(
q
-
1
))
# Fonction servant à decoder un caractère à l'aide de l'exponentiation rapide
def
Decode_Char
(
charADecoder
,
d
,
n
):
charDecode
=
Exponentiation_Rapide
(
charADecoder
,
d
,
n
)
print
(
"
Char à decoder : %d
\n
D : %d
\n
n : %d
\n\n\n
result : %d
"
%
(
charADecoder
,
d
,
n
,
charDecode
))
print
(
"
result = (CharADecoder ^ d) mod n
"
)
return
charDecode
This diff is collapsed.
Click to expand it.
code/main.py
+
43
−
5
View file @
04ee9293
...
@@ -15,12 +15,34 @@
...
@@ -15,12 +15,34 @@
from
functions
import
*
from
functions
import
*
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Initialisation et calcul des variables nécessaires au décodage du message
# Constante n à utiliser pour le déchiffrement
# Constante n à utiliser pour le déchiffrement
N
=
1882929457
N
=
1882929457
# Constante e à utiliser pour le déchiffrement
# Constante e à utiliser pour le déchiffrement
E
=
9719
E
=
9719
# Génération de p, en enregistrant le nombre d'itérations qui ont été nécessaires
nbIterations
,
P
=
Calculer_P
(
N
)
print
(
"
P = %d
"
%
P
)
# Calcul de la valeur Q
Q
=
N
/
P
print
(
"
Q = %d
"
%
Q
)
# Calcul de Phi
Phi
=
Calculer_Phi
(
P
,
Q
)
print
(
"
Phi = %d
"
%
Phi
)
# Calcule des coéfficients de Bézout entre E et Phi, et de leurs pgcd (même si nous l'utilisons pas dans ce cas précis)
pgcd
,
D
,
B
=
Euclide_Etendu
(
E
,
Phi
)
print
(
"
D x E + B x Phi = %d x %d + %d x %d = %d
"
%
(
D
,
E
,
B
,
Phi
,
pgcd
))
print
(
"
D = %d
"
%
D
)
D
=
D
%
Phi
print
(
"
D mod Phi = %d
"
%
D
)
# Message chiffré
# Message chiffré
message
=
[
message
=
[
1794310518
,
1794310518
,
...
@@ -65,10 +87,26 @@ message = [
...
@@ -65,10 +87,26 @@ message = [
656966934
656966934
]
]
# Test de la fonction Euclide Etendue : (Fonctionne correctement, 11.01.2022)
pgcd
,
x
,
y
=
EuclideEtendu
(
168
,
68
)
print
(
"
%d x 168 + %d x 68 = %d
"
%
(
x
,
y
,
pgcd
))
# Test de la fonction d'Exponentiation rapide : (Fonctionne correctement, 11.01.2022)
result
=
""
result
=
ExponentiationRapide
(
6
,
55
,
23
)
for
i
in
message
:
print
(
i
)
decodedChar
=
Decode_Char
(
i
,
D
,
N
)
print
(
decodedChar
)
print
(
chr
(
int
(
decodedChar
)))
print
(
"
---------------------------
"
)
# b = bin(decodedChar)
# b = str(b)
# b = b[2:]
# if len(b) % 8 != 0:
# add_0 = 8 - (len(b) % 8)
# b = ("0" * add_0) + b
# binary = bytearray()
# for i in range(0, len(b), 8):
# binary.append(int(b[i:i + 8], 2))
# binary = binary[::-1]
# result += binary.decode("utf-8")
print
(
result
)
print
(
result
)
\ 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