-2

I have a problem where the RSA implementation doesn't work in a specific line of code:

def chooseKeys():
    """
    Selects two random prime numbers from a list of prime numbers which has
    values that go up to 100k. It creates a text file and stores the two
    numbers there where they can be used later. Using the prime numbers,
    it also computes and stores the public and private keys in two separate
    files.
    """

    # choose two random numbers within the range of lines where
    # the prime numbers are not too small and not too big
    rand1 = random.randint(100, 300)


    rand2 = random.randint(100, 300)

    # store the txt file of prime numbers in a python list
    fo = open('primes-to-100k.txt', 'r')
    lines = fo.read().splitlines()
    fo.close()

    # store our prime numbers in these variables
    prime1 = int(lines[rand1])
    prime2 = int(lines[rand2])

    # compute n, totient, e
    n = prime1 * prime2
    totient = (prime1 - 1) * (prime2 - 1)
    e = chooseE(totient)

    # compute d, 1 < d < totient such that ed = 1 (mod totient)
    # e and d are inverses (mod totient)
    gcd, x, y = xgcd(e, totient)

    # make sure d is positive
    if (x < 0):
        d = x + totient
    else:
        d = x

    # write the public keys n and e to a file
    f_public = open('public_keys.txt', 'w')
    f_public.write(str(n) + '\n')
    f_public.write(str(e) + '\n')
    f_public.close()

    f_private = open('private_keys.txt', 'w')
    f_private.write(str(n) + '\n')
    f_private.write(str(d) + '\n')
    f_private.close()

the problem here is this line:

   # store the txt file of prime numbers in a python list
fo = open('primes-to-100k.txt', 'r')
lines = fo.read().splitlines()
fo.close()

it always says Exception has occurred: FileNotFoundError

[Errno 2] No such file or directory: 'primes-to-100k.txt'

so for that I tried to use the path for the file, didn't work, tried to use the relative path, that also didn't work, any solution for thi

President James K. Polk
  • 36,717
  • 16
  • 86
  • 116

1 Answers1

0

I don't see where the file primes-to-100k.txt is written to disk, only it being read in, nor can verify that the file does exist in the directory with your posted code. The exception truly means it cannot find that file at the specified path, it either doesn't exist or is the wrong path.

I worry about the security overall of the application, the built in random module works off of mathematical formula vs some of the python cryptography libraries like PyCryptodome's Random module that uses system process states etc for randomisation. The problem is that the mathematical formula tends to repeat numbers and is not truly be random. You will end up with repeat data. Opening you up to brute force.

Here are some examples from that library to do what your wanting securely:

https://pycryptodome.readthedocs.io/en/latest/src/examples.html

A link to more information about the random's module randomness:

Is random.sample truly random?

nknous
  • 66
  • 4
  • It's obviously a learning exercise, in the OPs case the low security of the randomizer is secondary to the completely non-existent security of using an RSA modulus the product of two primes < 10**5. – President James K. Polk Nov 30 '18 at 03:57