
Raise ValueError('no modular multiplicative inverse found') Raise ValueError('no suitable prime number found reached '.format(n))
# multiples of its witnesses to prepare for largerįor i, n in enumerate(generate_primes()):
# need it in the map, but we'll mark the next # Yield it and mark its first multiple that isn't # The running integer that's checked for primeness # indefinitely, but only as long as required by the current # This is memory efficient, as the sieve is not "run forward" # Maps composites to primes witnessing their compositeness. Generate an infinite sequence of prime numbers. Implement those parts as needed (random or prompting user for input) and just pass the result to the function for encryption.
I moved the input prompts outside of the encryption code. Invoke the main functions inside a if _name_ = '_main_'. I replaced the parts that read the prime number file with a prime number generator, but that is just because I didn't have the file at hand. Change this line d = calculate_d(phi, e)Īlso, since you asked for suggestions to improve your code, I made a few (a lot) improvements. It is solved when we invert the params of one of your function. I need the code to firstly encrypt the message with the encrypt function then decrypt it with the decrypt function, so the encrypted and original message should be displayed.Ĭould someone tell me whats wrong with my code (since I'm still in school, it may need to be simplified), any additional feedback would be greatly appreciated.Īfter a bit of debugging, the problem is that the function calculate_d() does not seem to calculate the right number. Q = int(input("Enter a key - this must be a prime number between 0 and 500:\n"))Įncrypted_message, N, e = encrypted, encrypted, encrypted P = int(input("Enter a key - this must be a prime number between 0 and 500:")) Message = input("Enter a message to encrypt:")Ĭhoice = input("Do you want to: \nA: enter a key \nB: use a random key?\n") The encryption function works to produce what looks like a feasible output but when I put it into the decryption function it either doesn't return the message correctly or gives me this error: ValueError: chr() arg not in range(0x110000)
I've based the code off of the formulae c = m^e(mod N) and (c^d)mod N. I'm currently coding a simplified RSA algorithm for a project at school but can't get it to work.