Given an RSA key (n,e,d), construct a program to encrypt and decrypt plaintext messages strings.

Background

RSA code is used to encode secret messages. It is named after Ron Rivest, Adi Shamir, and Leonard Adleman who published it at MIT in 1977. The advantage of this type of encryption is that you can distribute the number “



n


{\displaystyle n}

” and “



e


{\displaystyle e}

” (which makes up the Public Key used for encryption) to everyone. The Private Key used for decryption “



d


{\displaystyle d}

” is kept secret, so that only the recipient can read the encrypted plaintext.

The process by which this is done is that a message, for example “Hello World” is encoded as numbers (This could be encoding as ASCII or as a subset of characters 



a
=
01
,
b
=
02
,
.
.
.
,
z
=
26


{\displaystyle a=01,b=02,...,z=26}

). This yields a string of numbers, generally referred to as "numerical plaintext", “



P


{\displaystyle P}

”. For example, “Hello World” encoded with a=1,...,z=26 by hundreds would yield 



08051212152315181204


{\displaystyle 08051212152315181204}

.

The plaintext must also be split into blocks so that the numerical plaintext is smaller than 



n


{\displaystyle n}

 otherwise the decryption will fail.

The ciphertext, 



C


{\displaystyle C}

, is then computed by taking each block of 



P


{\displaystyle P}

, and computing

 



C
≡

P

e



mod


n


{\displaystyle C\equiv P^{e}\mod n}


Similarly, to decode, one computes

 



P
≡

C

d



mod


n


{\displaystyle P\equiv C^{d}\mod n}

 
To generate a key, one finds 2 (ideally large) primes 



p


{\displaystyle p}

 and 



q


{\displaystyle q}

. the value “



n


{\displaystyle n}

” is simply: 



n
=
p
×
q


{\displaystyle n=p\times q}

.
One must then choose an “



e


{\displaystyle e}

” such that 



gcd
(
e
,
(
p
−
1
)
×
(
q
−
1
)
)
=
1


{\displaystyle \gcd(e,(p-1)\times (q-1))=1}

. That is to say, 



e


{\displaystyle e}

 and 



(
p
−
1
)
×
(
q
−
1
)


{\displaystyle (p-1)\times (q-1)}

 are relatively prime to each other.

The decryption value 



d


{\displaystyle d}

 is then found by solving

 



d
×
e
≡
1

mod


(
p
−
1
)
×
(
q
−
1
)


{\displaystyle d\times e\equiv 1\mod (p-1)\times (q-1)}


The security of the code is based on the secrecy of the Private Key (decryption exponent) “



d


{\displaystyle d}

” and the difficulty in factoring “



n


{\displaystyle n}

”. Research into RSA facilitated advances in factoring and a number of factoring challenges. Keys of 768 bits have been successfully factored. While factoring of keys of 1024 bits has not been demonstrated, NIST expected them to be factorable by 2010 and now recommends 2048 bit keys going forward (see Asymmetric algorithm key lengths or NIST 800-57 Pt 1 Revised Table 4: Recommended algorithms and minimum key sizes).

Summary of the task requirements:

 Encrypt and Decrypt a short message or two using RSA with a demonstration key. 
 Implement RSA do not call a library.
 Encode and decode the message using any reversible method of your choice (ASCII or a=1,..,z=26 are equally fine). 
 Either support blocking or give an error if the message would require blocking)
 Demonstrate that your solution could support real keys by using a non-trivial key that requires large integer support (built-in or libraries). There is no need to include library code but it must be referenced unless it is built into the language. The following keys will be meet this requirement;however, they are NOT long enough to be considered secure:
 n = 9516311845790656153499716760847001433441357
 e = 65537
 d = 5617843187844953170308463622230283376298685
 Messages can be hard-coded into the program, there is no need for elaborate input coding.
 Demonstrate that your implementation works by showing plaintext, intermediate results, encrypted text, and decrypted text.


 Warning
Rosetta Code is not a place you should rely on for examples of code in critical roles, including security.
Cryptographic routines should be validated before being used.
For a discussion of limitations and please refer to Talk:RSA_code#Difference_from_practical_cryptographical_version.

module RSAMaker 
   where
import Data.Char ( chr )
 
encode :: String -> [Integer]
encode s = map (toInteger . fromEnum ) s
 
rsa_encode :: Integer -> Integer -> [Integer] -> [Integer]
rsa_encode n e numbers = map (\num -> mod ( num ^ e ) n ) numbers
 
rsa_decode :: Integer -> Integer -> [Integer] -> [Integer]
rsa_decode d n ciphers = map (\c -> mod ( c ^ d ) n ) ciphers
 
decode :: [Integer] -> String
decode encoded = map ( chr . fromInteger ) encoded 
 
divisors :: Integer -> [Integer]
divisors n = [m | m <- [1..n] , mod n m == 0 ]
 
isPrime :: Integer -> Bool
isPrime n = divisors n == [1,n]
 
totient :: Integer -> Integer -> Integer
totient prime1 prime2 = (prime1 - 1 ) * ( prime2 - 1 ) 
 
myE :: Integer -> Integer
myE tot = head [n | n <- [2..tot - 1] , gcd n tot == 1]
 
myD :: Integer -> Integer -> Integer  -> Integer
myD e n phi = head [d | d <- [1..n] , mod ( d * e ) phi == 1]
 
main = do 
   putStrLn "Enter a test text!"
   text <- getLine
   let primes = take 90 $ filter isPrime [1..]
       p1     = last primes
       p2     = last $ init primes
       tot    = totient p1 p2
       e      =  myE tot
       n   = p1  * p2 
       rsa_encoded  =  rsa_encode n e $ encode text
       d  =  myD e n tot
       encrypted = concatMap show rsa_encoded
       decrypted = decode $ rsa_decode d n rsa_encoded 
   putStrLn ("Encrypted: " ++ encrypted ) 
   putStrLn ("And now decrypted: " ++ decrypted )