for an assignment I need to write two function, one to RSA encrypt and one to decrypt. These functions take the key pair and modulo as parameters.
rsaencrypt :: (Integer, Integer) -> Integer -> Integer
rsaencrypt (e, m) x = x^e `mod` m
rsadecrypt :: (Integer, Integer) -> Integer -> Integer
rsadecrypt (d, m) x = x^d `mod` m
Let's suppose we have the key pair (5, 29) with a modulo of 91. if we want to encrypt the number 75 we get cyphertext 17, if we decrypt 17 we get the 75 back. All good so far. But when i try a different number, lets say 97. We get cyphertext 41, if we decrypt 41 we get 6. Why does my function only work on certain numbers?
CodePudding user response:
Looking at the definition of rsadecrypt
:
rsadecrypt (d, m) x = x^d `mod` m
It should be clear that all outputs will be between 0
and m-1
(inclusive). Since decryption definitely sits in that range, you cannot create an encryption for any message outside that range that will exactly round-trip.
...and, to connect the final two dots, 97
is outside the range of 0
to 91-1
, so it cannot be correctly encrypted (for this definition of "correct").