Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ricardo.dossanto1
tp_RSA
Commits
f87e2bf4
Commit
f87e2bf4
authored
Jan 17, 2022
by
martin.caron
Browse files
ça marche
parent
2ba5f6ec
Changes
1
Hide whitespace changes
Inline
Side-by-side
rsa.py
View file @
f87e2bf4
...
...
@@ -6,6 +6,7 @@ from itertools import combinations
from
collections
import
Counter
import
itertools
import
math
import
codecs
n
=
1329471887
e
=
6587
...
...
@@ -68,6 +69,34 @@ messages = [
1009905929
]
#lien https://interstices.info/nombres-premiers-et-cryptologie-lalgorithme-rsa/
##théorème: Soient p et q deux nombres premiers, et posons n = p × q.
#Soit e est un entier premier avec (p – 1) × (q – 1), alors il existe un entier d > 0 et un entier m tels que e × d + m × (p – 1)(q – 1) = 1.
#Notons au passage que si on choisit d positif et inférieur à (p – 1)(q – 1), alors d est unique.
#On note ak le nombre a élevé à la puissance k, c’est-à-dire le nombre a multiplié par lui-même k-1 fois.
#Pour tout entier a < n premier avec n, le reste de la division de a^(e×d) par n est égal à a.
#clé privé (p,q,d)
#clé publique (n,e)
#but: trouver la clé privé
#on calcule p et q
def
findPQ
(
n
):
a
=
b
=
(
int
)(
math
.
sqrt
(
n
))
if
a
%
2
==
0
:
b
-=
1
a
+=
1
for
p
in
range
(
a
,
int
(
n
/
3
),
2
):
for
q
in
range
(
b
,
2
,
-
2
):
if
p
*
q
==
n
:
return
p
,
q
def
bezout
(
a
,
b
):
if
b
==
0
:
return
1
,
0
else
:
u
,
v
=
bezout
(
b
,
a
%
b
)
return
v
,
u
-
(
a
//
b
)
*
v
# fonction PGCD:
def
pgcd
(
a
,
b
):
...
...
@@ -100,6 +129,7 @@ def EuclideEtendu2(a, b, x0, x1, y0, y1):
return
EuclideEtendu2
(
b
,
r
,
x1
,
x
,
y1
,
y
)
#calcule de bachet bezout
def
EuclideEtendu
(
a
,
b
):
if
pgcd
(
a
,
b
)
==
1
:
#a*u + b*v = pgcd(a,b)
...
...
@@ -130,18 +160,56 @@ def lpowmod(x, y, n):
"""puissance modulaire: (x**y)%n avec x, y et n entiers"""
result
=
1
while
y
>
0
:
if
y
&
1
>
0
:
if
y
and
1
>
0
:
result
=
(
result
*
x
)
%
n
y
>>=
1
y
>>=
1
x
=
(
x
*
x
)
%
n
return
result
p
,
q
=
EuclideEtendu2
(
n
,
e
,
1
,
0
,
0
,
1
)
#exponentiation rapide
def
fastExpo
(
a
,
x
,
n
):
if
a
==
0
:
return
0
if
x
==
0
:
return
1
r
=
1
e
=
x
b
=
a
%
n
while
e
>
0
:
y
=
e
%
2
r
=
(
r
*
pow
(
b
,
y
))
%
n
b
=
(
b
*
b
)
%
n
e
=
e
//
2
return
r
p
,
q
=
findPQ
(
n
)
#p,q = EuclideEtendu2(n, e, 1, 0, 0, 1)
nPrime
=
(
p
-
1
)
*
(
q
-
1
)
d
=
inverseMod
(
nPrime
,
e
)
print
(
d
)
#on a :e × d + m × nPrime = 1 : Bachet Bezout pour trouver m et d
#m,d = EuclideEtendu2(e, nPrime,1,0,0,1)
d
,
m
=
bezout
(
e
,
nPrime
)
#d = inverseMod(nPrime, e)
print
(
"d="
,
d
,
"m="
,
m
,
"p="
,
p
,
"q="
,
q
)
#for k in range(len(messages)):
# tmp = inverseMod(n,fastExpo(messages[k],d,n))
# print(tmp)
def
invers_bit
(
m
):
m
=
str
(
hex
(
m
)[
2
:])
# Prendre à partir du deuxième caractère (après le 0x)
m
=
""
.
join
(
reversed
([
m
[
i
:
i
+
2
]
for
i
in
range
(
0
,
len
(
m
),
2
)]))
# Inversion des octets 0x20654A ==> 0x4A6520
return
m
def
decode_text
(
m
):
m
=
codecs
.
decode
(
m
,
"hex"
).
decode
(
'utf-8'
)
# Décodage
return
m
s
=
""
for
item
in
messages
:
m
=
fastExpo
(
item
,
d
,
n
)
m
=
invers_bit
(
m
)
s
+=
decode_text
(
m
)
print
(
s
)
for
k
in
range
(
len
(
messages
)):
tmp
=
inverseMod
(
n
,
lpowmod
(
messages
[
k
],
d
,
n
))
print
(
tmp
)
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