RSA Programming Lab

Write a program that implements RSA encryption and decryption algorithms. The program should be able to perform either encryption or decryption. The program must take two inputs. The first input must be either 1 or 2, with 1 signaling encryption and 2 signaling decryption. The second input depends on the first input. In case of encryption, the second input consists of plaintext - a sequence of letters and additional information listed below which depends on specific encryption mode. In case of encryption your program must to output the numeric value of plaintext string, the numeric value of ciphertext and the ciphertext string. In case of decryption, the second input consists of Alice's two prime numbers p and q and exponent e, as well as Bob's ciphertext, which is a sequence of letters. In case of decryption your program must output d, numeric value of the plaintext and the original plaintext.

Assume that plaintext and ciphertext consist of UPPER CASE LETTERS ONLY with NO leading A's (Optional: you can assume that numeric values of letters are 1 to 26 and consider the system in base 27, instead of 26. This will resolve the problem with leading A's. Pay attention, in this case you would need to adjust the numeric value by 64, instead of 65, to get correct ASCII of characters and you would need to take in account a new range for plaintext). Your program should output encrypted or decrypted message accordingly with appropriate descriptive message.

Important: Your program must check validity of the input: p and q must be prime, e must be relatively prime to n = (p-1)*(q-1) and the plaintext must be between 0 and m = p*q

Important: both encryption and decryption will require to perform modular exponentiation. Your program is expected to implement successive squaring algorithm (fast modular exponentiation).

Pay attention, in case of encryption, your program must be able to work in two different modes:

References