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