From 18a4cf0e411b760ffc867551bcf24e59a287cc75 Mon Sep 17 00:00:00 2001 From: Zizhou Zhang Date: Sat, 9 Nov 2019 14:08:47 +1100 Subject: [PATCH 1/3] added RSA_factorization.py This algorithm can effectively factor RSA large prime N given public key e and private key d. --- ciphers/RSA_factorization.py | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 ciphers/RSA_factorization.py diff --git a/ciphers/RSA_factorization.py b/ciphers/RSA_factorization.py new file mode 100644 index 000000000000..75c289ab5c0d --- /dev/null +++ b/ciphers/RSA_factorization.py @@ -0,0 +1,45 @@ +""" +An RSA prime factor algorithm. + +The program can efficiently factor RSA prime number given the private key d and +public key e. +Source: on page 3 of https://crypto.stanford.edu/~dabo/papers/RSA-survey.pdf +large number can take minutes to factor, therefore are not included in doctest. +""" +import math +import random +from typing import List + + +def rsafactor(d: int, e: int, N: int) -> List[int]: + """ + This function returns the factors of N, where p*q=N + Return: [p, q] + + >>> rsafactor(3,16971,25777) + [149, 173] + >>> rsafactor(7331,11,27233) + [113, 241] + >>> rsafactor(4021,13,17711) + [89, 199] + """ + k = d * e - 1 + p = 0 + q = 0 + while p == 0: + g = random.randint(2, N - 1) + t = k + if t % 2 == 0: + t = t // 2 + x = (g ** t) % N + y = math.gcd(x - 1, N) + if x > 1 and y > 1: + p = y + q = N // y + return sorted([p, q]) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 40f88ff42295ca49f5a39aaf3b68dd6af5d9331b Mon Sep 17 00:00:00 2001 From: Zizhou Zhang Date: Sat, 9 Nov 2019 23:40:12 +1100 Subject: [PATCH 2/3] Rename RSA_factorization.py to rsa_factorization.py --- ciphers/{RSA_factorization.py => rsa_factorization.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ciphers/{RSA_factorization.py => rsa_factorization.py} (100%) diff --git a/ciphers/RSA_factorization.py b/ciphers/rsa_factorization.py similarity index 100% rename from ciphers/RSA_factorization.py rename to ciphers/rsa_factorization.py From 6f7959cd3aff3975012bcf8352555b551d5c8080 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 9 Nov 2019 15:02:11 +0100 Subject: [PATCH 3/3] Add definitions for d, e, and N --- ciphers/rsa_factorization.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ciphers/rsa_factorization.py b/ciphers/rsa_factorization.py index 75c289ab5c0d..58bdc554a861 100644 --- a/ciphers/rsa_factorization.py +++ b/ciphers/rsa_factorization.py @@ -15,12 +15,18 @@ def rsafactor(d: int, e: int, N: int) -> List[int]: """ This function returns the factors of N, where p*q=N Return: [p, q] + + We call N the RSA modulus, e the encryption exponent, and d the decryption exponent. + The pair (N, e) is the public key. As its name suggests, it is public and is used to + encrypt messages. + The pair (N, d) is the secret key or private key and is known only to the recipient + of encrypted messages. - >>> rsafactor(3,16971,25777) + >>> rsafactor(3, 16971, 25777) [149, 173] - >>> rsafactor(7331,11,27233) + >>> rsafactor(7331, 11, 27233) [113, 241] - >>> rsafactor(4021,13,17711) + >>> rsafactor(4021, 13, 17711) [89, 199] """ k = d * e - 1