By visualizing the plaintext as a set of numbers, for
instance the ASCII codes of each character, or group of characters. If we
denote any given number in this sequence as m. The resultant cipher text is
also a set of numbers, any of which is denoted by the variable c
From one of the Euler’s theorems, we know that for all
integers m,
med = m (mod n)
Therefore, provided that 0 <= m < n,
med (mod n) = m
To encrypt a message m, we perform the following
algorithm:
F(m) = me (mod n) = c where F(m) denotes the
encryption algorithm.
To decipher the cipher text c with the private key d, we
perform the following algorithm:
F(c) = cd (mod n) = med (mod n) = m1 (mod n) = m
where F( ) denotes the decryption algorithm.
The variable ‘e’ is known as the public key, and ‘d’ is
the private key.
‘n’ is used in both encryption and decryption.
Sample application
This application demonstrates how to implement RSA in .NET
from mathematical principles. It is important to note that character-by-character
RSA encryption is just as susceptible to frequency analysis as any simple
symmetric cipher. Therefore it is extremely important to pre-encrypt the
message using cryptanalysis-resistant encryption, such as the book cipher.
The symmetric key can be packaged along with the encrypted message prior to
RSA encryption. This pre-encryption step is omitted from this example for
simplicity.
Start a new project as before, and draw a form with 6
standard text boxes, and three multi-line textboxes. Name them, txtP,
txtQ,
txtPhi,
txtE,
txtN,
txtD
respectively. The three multi-line textboxes should be named txtPlain,
txtEncrypt,
and txtDecrypt
Drag three buttons onto the form; name them cmdKeyGen,
cmdEncrypt,
and cmdDecrypt
Add the following public variables to the code
Page 1
Page 3