Saataa andagii !
This commit is contained in:
parent
43a72c3fb7
commit
75d0a97501
6 changed files with 10 additions and 8 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
cr/
|
cr/
|
||||||
.venv/
|
.venv/
|
||||||
|
*.pdf
|
Binary file not shown.
Binary file not shown.
|
@ -10,8 +10,10 @@ def modpow(a: int, b: int, m: int) -> int:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def gcd(a: int, b: int) -> int:
|
def gcd(a: int, b: int) -> int:
|
||||||
|
# if b = 0 return a
|
||||||
if b == 0:
|
if b == 0:
|
||||||
return a
|
return a
|
||||||
|
# return gcd(b, a % b) because gcd(a, b) = gcd(b, rem(a, b))
|
||||||
return gcd(b, a % b)
|
return gcd(b, a % b)
|
||||||
|
|
||||||
def mod_inverse(A: int, M: int) -> int:
|
def mod_inverse(A: int, M: int) -> int:
|
||||||
|
@ -21,18 +23,13 @@ def mod_inverse(A: int, M: int) -> int:
|
||||||
if (M == 1):
|
if (M == 1):
|
||||||
return 0
|
return 0
|
||||||
while (A > 1):
|
while (A > 1):
|
||||||
# q is quotient
|
|
||||||
q = A // M
|
q = A // M
|
||||||
t = M
|
t = M
|
||||||
# m is remainder now, process
|
|
||||||
# same as Euclid's algo
|
|
||||||
M = A % M
|
M = A % M
|
||||||
A = t
|
A = t
|
||||||
t = y
|
t = y
|
||||||
# Update x and y
|
|
||||||
y = x - q * y
|
y = x - q * y
|
||||||
x = t
|
x = t
|
||||||
# Make x positive
|
|
||||||
if (x < 0):
|
if (x < 0):
|
||||||
x = x + m0
|
x = x + m0
|
||||||
return x
|
return x
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
# Python RSA implementation
|
# Python RSA implementation
|
||||||
|
|
||||||
import random
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import lib.arithmetics as arithm
|
import lib.arithmetics as arithm
|
||||||
import lib.miller_rabin as miller
|
import lib.miller_rabin as miller
|
||||||
|
|
||||||
|
@ -16,15 +13,21 @@ def get_p_and_q(n: int) -> (int, int):
|
||||||
return (p, q)
|
return (p, q)
|
||||||
|
|
||||||
def get_keys(l: int) -> int:
|
def get_keys(l: int) -> int:
|
||||||
|
# Get p and q
|
||||||
p, q = get_p_and_q(l // 2)
|
p, q = get_p_and_q(l // 2)
|
||||||
|
# Compute n and phy(n)
|
||||||
n = p * q
|
n = p * q
|
||||||
phy_n = (p - 1) * (q - 1)
|
phy_n = (p - 1) * (q - 1)
|
||||||
|
# Chose e = 65537
|
||||||
e = 65537
|
e = 65537
|
||||||
|
# Ensure e fits with the others numbers
|
||||||
while arithm.gcd(e, phy_n) != 1:
|
while arithm.gcd(e, phy_n) != 1:
|
||||||
p, q = get_p_and_q(l // 2)
|
p, q = get_p_and_q(l // 2)
|
||||||
n = p * q
|
n = p * q
|
||||||
phy_n = (p - 1) * (q - 1)
|
phy_n = (p - 1) * (q - 1)
|
||||||
|
# Compute d
|
||||||
d = arithm.mod_inverse(e, phy_n)
|
d = arithm.mod_inverse(e, phy_n)
|
||||||
|
# Return e, d and n
|
||||||
return (e, d, n)
|
return (e, d, n)
|
||||||
|
|
||||||
def encrypt(m: int, e: int, n: int) -> int:
|
def encrypt(m: int, e: int, n: int) -> int:
|
||||||
|
|
1
main.py
1
main.py
|
@ -9,6 +9,7 @@ LEN = BITS // 5
|
||||||
def encode(text):
|
def encode(text):
|
||||||
sum = 0
|
sum = 0
|
||||||
for i in range(len(text)):
|
for i in range(len(text)):
|
||||||
|
# Codage de la chaîne en base 27
|
||||||
sum += ALPH[text[i]] * 27 ** i
|
sum += ALPH[text[i]] * 27 ** i
|
||||||
return sum
|
return sum
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue