EX of RSA Algorithm
EX of RSA Algorithm
EX of RSA Algorithm
Example 1
This is actually the smallest possible value for the modulus n for which the RSA algorithm works.
Now if we calculate the ciphertext c for all the possible values of m (0 to 32), we get
m 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
c 0 1 8 27 31 26 18 13 17 3 10 11 12 19 5 9 4
m 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
c 29 24 28 14 21 22 23 30 16 20 15 7 2 6 25 32
Note that all 33 values of m (0 to 32) map to a unique code c in the same range in a sort of random
manner. In this case we have nine values of m that map to the same value of c - these are known as
unconcealed messages. m = 0 and 1 will always do this for any N, no matter how large. But in practice,
higher values shouldn't be a problem when we use large values for N.
If we wanted to use this system to keep secrets, we could let A=2, B=3, ..., Z=27. (We specifically avoid
0 and 1 here for the reason given above). Thus the plaintext message "HELLOWORLD" would be
represented by the set of integers m1, m2, ...
{9,6,13,13,16,24,16,19,13,5}
Using our table above, we obtain ciphertext integers c1, c2, ...
{3,18,19,19,4,30,4,28,19,26}
Note that this example is no more secure than using a simple Caesar substitution cipher, but it serves to
illustrate a simple example of the mechanics of RSA encryption.
Remember that calculating m^e mod n is easy, but calculating the inverse c^-e mod n is very difficult, well,
for large n's anyway. However, if we can factor n into its prime factors p and q, the solution becomes
easy again, even for large n's. Obviously, if we can get hold of the secret exponent d, the solution is
easy, too.
Example 2
This time, to make life slightly less easy for those who can crack simple Caesar substitution codes, we
will group the characters into blocks of three and compute a message representative integer for each
block.
ATTACKxATxSEVEN = ATT ACK XAT XSE VEN
In the same way that a decimal number can be represented as the sum of powers of ten, e.g.
135 = 1 x 10^2 + 3 x 10^1 + 5,
we could represent our blocks of three characters in base 26 using A=0, B=1, C=2, ..., Z=25
For this example, to keep things simple, we'll not worry about numbers and punctuation characters, or
what happens with groups AAA or AAB.
In this system of encoding, the maximum value of a group (ZZZ) would be 26^3-1 = 17575, so we require
a modulus n greater than this value.
1. We "generate" primes p=137 and q=131 (we cheat by looking for suitable primes around √n)
2. n = pq = 137.131 = 17947
phi = (p-1)(q-1) = 136.130 = 17680
3. Select e = 3
check gcd(e, p-1) = gcd(3, 136) = 1, OK and
check gcd(e, q-1) = gcd(3, 130) = 1, OK.
4. Compute d = e^-1 mod phi = 3^-1 mod 17680 = 11787.
5. Hence public key, (n, e) = (17947, 3) and private key (n, d) = (17947, 11787).
Question: Why couldn't we use e=17 here?
To help you carry out these modular arithmetic calculations, download our free modular arithmetic
command line programs.
Note that this is still a very insecure example. Starting with the knowledge that the modulus 17947 is
probably derived from two prime numbers close to its square root, a little testing of suitable candidates
from a table of prime numbers would get you the answer pretty quickly. Or just work methodically through
the table of prime numbers dividing n by each value until you get no remainder. You could also write a
simple computer program to factor n that just divides by every odd number starting from 3 until it reaches
a number greater than the square root of n.
Example 3
The reasons why this algorithm works are discussed in the mathematics section. Its security comes from
the computational difficulty of factoring large numbers. To be secure, very large numbers must be used
for p and q - 100 decimal digits at the very least.
Key Generation
1) Generate two large prime numbers, p and q
To make the example easy to follow I am going to use small numbers, but this is not secure. To find
random primes, we start at a random number and go up ascending odd numbers until we find a prime.
Lets have:
p=7
q = 19
2) Let n = pq
n = 7 * 19
= 133
3) Let m = (p - 1)(q - 1)
m = (7 - 1)(19 - 1)
= 6 * 18
= 108
e coprime to m, means that the largest number that can exactly divide both e and m (their greatest
common divisor, or gcd) is 1. Euclid's algorithm is used to find the gcd of two numbers, but the details
are omitted here.
This is equivalent to finding d which satisfies de = 1 + nm where n is any integer. We can rewrite this as
d = (1 + nm) / e. Now we work through values of n until an integer solution for e is found:
n = 0 => d = 1 / 5 (no)
n = 1 => d = 109 / 5 (no)
n = 2 => d = 217 / 5 (no)
n = 3 => d = 325 / 5
= 65 (yes!)
To do this with big numbers, a more sophisticated algorithm called extended Euclid must be used.
n = 133 n = 133
e=5 d = 65
Communication
Encryption
The message must be a number less than the smaller of p and q. However, at this point we don't know p
or q, so in practice a lower bound on p and q must be published. This can be somewhat below their true
value and so isn't a major security concern. For this example, lets use the message "6".
C = Pe % n
= 65 % 133
= 7776 % 133
= 62
Decryption
This works very much like encryption, but involves a larger exponation, which is broken down into
several steps.
P = Cd % n
= 6265 % 133
= 62 * 6264 % 133
= 62 * (622)32 % 133
= 62 * 384432 % 133
= 62 * (3844 % 133)32 % 133
= 62 * 12032 % 133
We now repeat the sequence of operations that reduced 6265 to 12032 to reduce the exponent down to 1.
= 62 * 3616 % 133
= 62 * 998 % 133
= 62 * 924 % 133
= 62 * 852 % 133
= 62 * 43 % 133
= 2666 % 133
=6
Example 4
()
Only the public key (e,n) is published; all the other numbers involved (p,q,φ,d) must be kept private! The
main property of this construction is that it is 'difficult' to compute d just from the numbers e and n
('difficult' in the sense that the computation is more and more time-consuming the larger numbers are
involved). More precisely:
RSA Encryption
First of all we need the public key of the person to whom we want to send the message:
(e,n) = ( , ) (Enter appropriate values or )
Next we need the message. For simplicity let us demonstrate this here with just one letter. (In secure
applications letters are never encrypted individually, but in whole blocks.)
Pick a letter to cipher:
Before we can encrypt this letter we must :
m= (here we take just the index in the alphabet)
RSA Decryption
First of all we need the private key of the person who got the encrypted message:
(d,n) = ( , ) (Enter appropriate values or )
Only the public key (e,n) is published; all the other numbers involved (p,q,φ,d) must be kept private! The
main property of this construction is that it is 'difficult' to compute d just from the numbers e and n
('difficult' in the sense that the computation is more and more time-consuming the larger numbers are
involved). More precisely:
RSA Encryption
First of all we need the public key of the person to whom we want to send the message:
(e,n) = ( , ) (Enter appropriate values or )
Next we need the message. For simplicity let us demonstrate this here with just one letter. (In secure
applications letters are never encrypted individually, but in whole blocks.)
Pick a letter to cipher:
Before we can encrypt this letter we must :
m= (here we take just the index in the alphabet)
RSA Decryption
First of all we need the private key of the person who got the encrypted message:
(d,n) = ( , ) (Enter appropriate values or )
Example 5
Example
Suppose we wish to encode the plaintext message Pi = 3 (that is, under our encoding some letter has
been assigned the numerical value 3) subject to our choices of p=11, q=17 (thus, n=187) and E=7 (note
that 7 is relatively prime to 187.) Then the ciphertext Ci is given by
Example 6
D=23. Note that this is somewhat of a non-trivial calculation. For a given calculation the exponent may be
quite large (this is a problem we will also face when doing the decryption process.) However, using the
modulus and writing the exponent in powers of 2, as we did here, can greatly speed this calculation.
Example 7
Why was there a warning in the previous example? If you have been closely examining what has taken
place in the RSA algorithm you may have noticed that although we know the factorization of n (since we
choose the prime factors p and q) and hence , we may not have an easy time
determing , which requires us to know all the factors of what could be a
very large number. This seems to contradict the polynomial time needed to solve for the key. The
solution is (and is a key -- unintentional pun -- element of the RSA algorithm) that the formula for D,
although concise, is not the way the solution is found in practice. The actual method of solution (which
does require polynomial time computation) is based on the Euclidean algorithm.
In algebraic terms, we say we have written 1 as a linear combination of 7 and 160. Since 160 is the
modulus, we have
Hence D=23! Thus the real key to the solution of D is knowing which requires the knowledge of the
factorization of n since .
Example. 8
Theory :
example
Here is an example of RSA encryption and decryption. The parameters used here are artificially small,
but you can also use OpenSSL to generate and examine a real keypair.
p = 61 and q = 53
2. Compute
n = 61 * 53 = 3233
e = 17
5. Choose to satisfy
d = 2753
17 * 2753 = 46801 = 1 + 15 * 3120.
The public key is (n = 3233, e = 17). For a padded message the encryption function is: