The RSA algorithm is named after Ron Rivest, Adi Shamir and Len Adleman, who invented it in 1977
[RIVE78].
The basic technique was first discovered in 1973 by
Clifford Cocks [COCK73]
of CESG (part of the British GCHQ) but this was a secret until 1997.
The patent taken out by RSA Labs has expired.
The RSA cryptosystem is the most widely-used public key cryptography algorithm in the world. It can be used to encrypt a message without the need to exchange a secret key separately.
The RSA algorithm can be used for both public key encryption and digital signatures. Its security is based on the difficulty of factoring large integers.
Party A can send an encrypted message to party B without any prior exchange of secret keys. A just uses B's public key to encrypt the message and B decrypts it using the private key, which only he knows. RSA can also be used to sign a message, so A can sign a message using their private key and B can verify it using A's public key.
We look into the mathematics behind the algorithm on our RSA Theory page.
Algorithm: Generate an RSA key pair.
INPUT: Required modulus bit length, k.
OUTPUT: An RSA key pair ((N,e), d) where N is the modulus, the product of two primes (N=pq) not exceeding k bits in length; e is the public exponent, a number less than and coprime to (p-1)(q-1); and d is the private exponent such that ed ≡ 1 (mod (p-1)(q-1)).
The function
If you've chosen e = 65537 then the chances are that the first prime returned in steps (3) and (6) will pass the tests in steps (4) and (7), so each repeat-until loop will most likely just take one iteration. The final value of N may have a bit length slightly short of the target k. This actually does not matter too much (providing the message m is always < N), but some schemes require a modulus of exact length. If this is the case, then just repeat the entire algorithm until you get one. It should not take too many goes. Alternatively, use the trick setting the two highest bits in the prime candidates described in note 1.
For more on the theory and mathematics behind the algorithm, see the RSA Theory page.
There is one small area of confusion in defining the key length. One convention is that the key length is the position of the most significant bit in n that has value '1', where the least significant bit is at position 1. Equivalently, key length = ceiling(log2(n+1)). The other convention, sometimes used, is that the key length is the number of bytes needed to store n multiplied by eight, i.e. ceiling(log256(n+1))*8.
The key used in the RSA Example paper [KALI93] is an example. In hex form the modulus is
The above table is a few years old now and may be out of date. Existing cryptographic algorithms only get weaker as attacks get better.
The calculation a = be mod n is known as modular exponentiation and one efficient method to carry this out on a computer is the binary left-to-right method. To solve y = x^e mod n let e be represented in base 2 as
e = e(k-1)e(k-2)...e(1)e(0)
where e(k-1) is the most significant non-zero bit and bit e(0) the least.
The bits in the decryption exponent d, however, will not be so convenient and so decryption using the standard method of modular exponentiation will take longer than encryption. Don't make the mistake of trying to contrive a small value for d; it is not secure.
An alternative method of representing the private key uses the The Chinese Remainder Theorem (CRT).
The private key is represented as a quintuple (p, q, dP, dQ, and qInv), where p and q are prime factors of n, dP and dQ are known as the CRT exponents, and qInv is the CRT coefficient. The CRT method of decryption is about four times faster overall than calculating m = cd mod n. The extra values for the private key are:-
[2008-09-02] Chris Becke has pointed out that most large integer packages will fail when computing
c = me mod n = 73 mod 33 = 343 mod 33 = 13.
Hence the ciphertext c = 13.
To check decryption we compute
m' = cd mod n = 137 mod 33 = 7.
Note that we don't have to calculate the full value of 13 to the power 7 here. We can make use of the fact that
a = bc mod n = (b mod n).(c mod n) mod n
so we can break down a potentially large number into its components and combine the results of easier, smaller calculations to calculate the final value.
One way of calculating m' is as follows:-
Note that any number can be expressed as a sum of powers of 2. So first compute values of 132, 134, 138, ... by repeatedly squaring successive values modulo 33.
132 = 169 ≡ 4, 134 = 4.4 = 16, 138 = 16.16 = 256 ≡ 25.
Then, since 7 = 4 + 2 + 1, we have m' = 137 = 13(4+2+1) = 134.132.131
≡ 16 x 4 x 13 = 832 ≡ 7 mod 33
Now if we calculate the ciphertext c for all the possible values of m (0 to 32), we get
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, ...
For this example, to keep things simple, we'll limit our characters to the letters A to Z and the space character.
135 = 1 x 102 + 3 x 101 + 5,
we can represent our blocks of three characters as the sum of powers of 27 using SPACE=0, A=1, B=2, C=3, .. E=5, .. K=11, .. N=14, .. S=19, T=20, .. V=22, ..., Z=26.
c = me mod n = 12893 mod 25777 = 18524.
Overall, our plaintext
To help you carry out these modular arithmetic calculations, download our free modular arithmetic command line programs.
For example, to compute
Question: Why can't we use this method of encoding integers into blocks of three letters to encode the ciphertext?
Also, rather than trying to represent the plaintext as an integer directly, we generate a random session key and use that to encrypt the plaintext with a conventional, much faster symmetrical algorithm like Triple DES or AES-128. We then use the much slower public key encryption algorithm to encrypt just the session key.
The sender A then transmits a message to the recipient B in a format something like this:-
If Mallory intercepts the transmission, he can either try and crack the conventionally-encrypted plaintext directly, or he can try and decrypt the encryped session key and then use that in turn. Obviously, this system is as strong as its weakest link.
When signing, it is usual to use RSA to sign the message digest of the message rather than the message itself. A one-way hash function like SHA-1 or SHA-256 is used. The sender A then sends the signed message to B in a format like this
The RSA cryptosystem is the most widely-used public key cryptography algorithm in the world. It can be used to encrypt a message without the need to exchange a secret key separately.
The RSA algorithm can be used for both public key encryption and digital signatures. Its security is based on the difficulty of factoring large integers.
Party A can send an encrypted message to party B without any prior exchange of secret keys. A just uses B's public key to encrypt the message and B decrypts it using the private key, which only he knows. RSA can also be used to sign a message, so A can sign a message using their private key and B can verify it using A's public key.
We look into the mathematics behind the algorithm on our RSA Theory page.
Key Generation Algorithm
This is the original algorithm.- Generate two large random primes, p and q, of approximately equal size such that their product n = pq is of the required bit length, e.g. 1024 bits. [See note 1].
- Compute n = pq and (phi) φ = (p-1)(q-1). [See note 6].
- Choose an integer e, 1 < e < phi, such that gcd(e, phi) = 1. [See note 2].
- Compute the secret exponent d, 1 < d < phi, such that ed ≡ 1 (mod phi). [See note 3].
- The public key is (n, e) and the private key (d, p, q). Keep all the values d, p, q and phi secret. [We prefer sometimes to write the private key as (n, d) because you need the value of n when using d. Other times we might write the key pair as ((N, e), d).]
- n is known as the modulus.
- e is known as the public exponent or encryption exponent or just the exponent.
- d is known as the secret exponent or decryption exponent.
A practical key generation algorithm
Incorporating the advice given in the notes below, a practical algorithm to generate an RSA key pair is given below. Typical bit lengths are k = 1024, 2048, 3072, 4096,..., with increasing computational expense for larger values. You will not go far wrong if you choose e as 65537 (=0x10001) in step (1).Algorithm: Generate an RSA key pair.
INPUT: Required modulus bit length, k.
OUTPUT: An RSA key pair ((N,e), d) where N is the modulus, the product of two primes (N=pq) not exceeding k bits in length; e is the public exponent, a number less than and coprime to (p-1)(q-1); and d is the private exponent such that ed ≡ 1 (mod (p-1)(q-1)).
- Select a value of e from {3, 5, 17, 257, 65537}
- repeat
- p ← genprime(k/2)
- until (p mod e) ≠ 1
- repeat
- q ← genprime(k - k/2)
- until (q mod e) ≠ 1
- N ← pq
- L ← (p-1)(q-1)
- d ← modinv(e, L)
- return (N, e, d)
The function
genprime(b)
returns a prime of exactly b bits, with the bth bit set to 1.
Note that the operation k/2 is integer division giving the integer quotient with no fraction.
If you've chosen e = 65537 then the chances are that the first prime returned in steps (3) and (6) will pass the tests in steps (4) and (7), so each repeat-until loop will most likely just take one iteration. The final value of N may have a bit length slightly short of the target k. This actually does not matter too much (providing the message m is always < N), but some schemes require a modulus of exact length. If this is the case, then just repeat the entire algorithm until you get one. It should not take too many goes. Alternatively, use the trick setting the two highest bits in the prime candidates described in note 1.
Encryption
Sender A does the following:-- Obtains the recipient B's public key (n, e).
- Represents the plaintext message as a positive integer m, 1 < m < n [see note 4].
- Computes the ciphertext c = me mod n.
- Sends the ciphertext c to B.
Decryption
Recipient B does the following:-- Uses his private key (n, d) to compute m = cd mod n.
- Extracts the plaintext from the message representative m.
Digital signing
Sender A does the following:-- Creates a message digest of the information to be sent.
- Represents this digest as an integer m between 1 and n-1. [See note 5].
- Uses her private key (n, d) to compute the signature s = md mod n.
- Sends this signature s to the recipient, B.
Signature verification
Recipient B does the following:-- Uses sender A's public key (n, e) to compute integer v = se mod n.
- Extracts the message digest from this integer.
- Independently computes the message digest of the information that has been signed.
- If both message digests are identical, the signature is valid.
Notes on practical applications
- To generate the primes p and q,
generate a random number of bit length k/2 where
k is the required bit length of the modulus n; set the low bit (this ensures the number is odd)
and set the two highest bits (this ensures that the high bit of n is also set);
check if prime (use the Rabin-Miller test);
if not, increment the number
by two and check again until you find a prime. This is p.
Repeat for q starting with a random integer of length k-k/2.
If p<q, swop p and q (this only matters if you intend using the CRT form of the private key).
In the extremely unlikely event that p = q, check your random number generator.
Alternatively, instead of incrementing by 2, just generate another random number each time.
There are stricter rules in ANSI X9.31 to produce strong primes
and other restrictions on p and q to minimise the possibility of known techniques being
used against the algorithm. There is much argument about this topic. It is probably better just to use a longer key length.
- In practice, common choices for e are 3, 5, 17, 257 and 65537 (216+1).
These particular values are chosen because they are primes and make the
modular exponentiation operation faster, having only two bits of value
1.
Aside: These five numbers are the first five Fermat numbers, referred to as F0 to F4, where . Just be careful, these first five Fermat numbers are prime ("Fermat primes"), but the numbers F5 and above are not prime. For example, F5 = 4294967297 = 641 × 6700417.The usual choice for e is F4 = 65537 = 0x10001. Also, having chosen e, it is simpler to test whether gcd(e, p-1)=1 and gcd(e, q-1)=1 while generating and testing the primes in step 1. Values of p or q that fail this test can be rejected there and then.Even better: if e is an odd prime then you can do the less-expensive test (p mod e) ≠ 1 instead of gcd(p-1,e) = 1.Why is that? If e is prime then gcd(p-1, e) > 1 if and only if p-1 is a multiple of e. That is, if p - 1 ≡ 0 (mod e) or p ≡ 1 (mod e). Hence gcd(p-1, e) = 1 ⇔ p mod e ≠ 1.
- To compute the value for d, use the Extended Euclidean Algorithm to calculate d = e-1 mod phi, also written d = (1/e) mod phi. This is known as modular inversion. Note that this is not integer division. The modular inverse d is defined as the integer value such that ed = 1 mod phi. It only exists if e and phi have no common factors.
- When representing the plaintext octets as the representative integer m, it is important to add random padding characters to make the size of the integer m large and less susceptible to certain types of attack. If m = 0 or 1 or n-1 there is no security as the ciphertext has the same value. For more details on how to represent the plaintext octets as a suitable representative integer m, see PKCS#1 Schemes below or the reference itself [PKCS1]. It is important to make sure that m < n otherwise the algorithm will fail. This is usually done by making sure the first octet of m is equal to 0x00.
- Decryption and signing are identical as far as the mathematics is concerned as both use the private key.
Similarly, encryption and verification both use the same mathematical operation with the public key.
That is, mathematically, for m < n,
m = (me mod n)d mod n = (md mod n)e mod nHowever, note these important differences in implementation:-
- The signature is derived from a message digest of the original information. The recipient will need to follow exactly the same process to derive the message digest, using an identical set of data.
- The recommended methods for deriving the representative integers are different for encryption and signing (encryption involves random padding, but signing uses the same padding each time).
- The original definition of RSA uses the Euler totient function
φ(n) = (p-1)(q-1). More recent standards use the Charmichael function λ(n) = lcm(p-1, q-1) instead.
λ(n) is smaller than φ(n) and divides it. The value of d' computed by d' = e-1 mod λ(n)
is usually different from that derived by d = e-1 mod φ(n), but the end result is the same.
Both d and d' will decrypt a message me mod n and both will give the same signature value
s = md mod n = md' mod n. To compute λ(n), use the relation
λ(n) = (p-1)(q-1) / gcd(p-1, q-1).
- You might ask if there is a way to find the factors of n given just d and e. This is possible.
Summary of RSA
- n = pq, where p and q are distinct primes.
- phi, φ = (p-1)(q-1)
- e < n such that gcd(e, phi)=1
- d = e-1 mod phi.
- c = me mod n, 1<m<n.
- m = cd mod n.
Key length
When we talk about the key length of an RSA key, we are referring to the length of the modulus, n, in bits. The minimum recommended key length for a secure RSA transmission is currently 1024 bits. A key length of 512 bits is now no longer considered secure, although cracking it is still not a trivial task for the likes of you and me. The longer your information is needed to be kept secure, the longer the key you should use. Keep up to date with the latest recommendations in the security journals.There is one small area of confusion in defining the key length. One convention is that the key length is the position of the most significant bit in n that has value '1', where the least significant bit is at position 1. Equivalently, key length = ceiling(log2(n+1)). The other convention, sometimes used, is that the key length is the number of bytes needed to store n multiplied by eight, i.e. ceiling(log256(n+1))*8.
The key used in the RSA Example paper [KALI93] is an example. In hex form the modulus is
0A 66 79 1D C6 98 81 68 DE 7A B7 74 19 BB 7F B0 C0 01 C6 27 10 27 00 75 14 29 42 E1 9A 8D 8C 51 D0 53 B3 E3 78 2A 1D E5 DC 5A F4 EB E9 94 68 17 01 14 A1 DF E6 7C DC 9A 9A F5 5D 65 56 20 BB ABThe most significant byte 0x0A in binary is
00001010'B
.
The most significant bit is at position 508, so its key length is 508
bits.
On the other hand, this value needs 64 bytes to store it, so the key
length could also be referred to by some as 64 x 8 = 512 bits.
We prefer the former method. You can get into difficulties with the
X9.31 method for signatures if you use the latter convention.
Minimum key lengths
The following table is taken from NIST's Recommendation for Key Management [NIST-80057]. It shows the recommended comparable key sizes for symmetrical block ciphers (AES and Triple DES) and the RSA algorithm. That is, the key length you would need to use to have comparable security.Symmetric key algorithm | Comparable RSA key length | Comparable hash function | Bits of security |
---|---|---|---|
2TDEA* | 1024 | SHA-1 | 80 |
3TDEA | 2048 | SHA-224 | 112 |
AES-128 | 3072 | SHA-256 | 128 |
AES-192 | 7680 | SHA-384 | 192 |
AES-256 | 15360 | SHA-512 | 256 |
* 2TDEA is 2-key triple DES - see What's two-key triple DES encryption.
Note just how huge (and impractical) an RSA key needs to be for comparable security with AES-192 or AES-256
(although these two algorithms have had some
weaknesses exposed recently; AES-128 is unaffected).
The above table is a few years old now and may be out of date. Existing cryptographic algorithms only get weaker as attacks get better.
Computational Efficiency and the Chinese Remainder Theorem (CRT)
Key generation is only carried out occasionally and so computational efficiency is less of an issue.The calculation a = be mod n is known as modular exponentiation and one efficient method to carry this out on a computer is the binary left-to-right method. To solve y = x^e mod n let e be represented in base 2 as
e = e(k-1)e(k-2)...e(1)e(0)
where e(k-1) is the most significant non-zero bit and bit e(0) the least.
set y = x for bit j = k - 2 downto 0 begin y = y * y mod n /* square */ if e(j) == 1 then y = y * x mod n /* multiply */ end return yThe time to carry out modular exponentation increases with the number of bits set to one in the exponent e. For encryption, an appropriate choice of e can reduce the computational effort required to carry out the computation of c = me mod n. Popular choices like 3, 17 and 65537 are all primes with only two bits set: 3 = 0011'B, 17 = 0x11, 65537 = 0x10001.
The bits in the decryption exponent d, however, will not be so convenient and so decryption using the standard method of modular exponentiation will take longer than encryption. Don't make the mistake of trying to contrive a small value for d; it is not secure.
An alternative method of representing the private key uses the The Chinese Remainder Theorem (CRT).
The private key is represented as a quintuple (p, q, dP, dQ, and qInv), where p and q are prime factors of n, dP and dQ are known as the CRT exponents, and qInv is the CRT coefficient. The CRT method of decryption is about four times faster overall than calculating m = cd mod n. The extra values for the private key are:-
dP = (1/e) mod (p-1) dQ = (1/e) mod (q-1) qInv = (1/q) mod p where p > qwhere the
(1/e)
notation means the modular inverse (see note 3 above).
These values are pre-computed and saved along with p and q as the private key.
To compute the message m given c do the following:-
m1 = c^dP mod p m2 = c^dQ mod q h = qInv(m1 - m2) mod p m = m2 + hqEven though there are more steps in this procedure, the modular exponentation to be carried out uses much shorter exponents and so it is less expensive overall.
[2008-09-02] Chris Becke has pointed out that most large integer packages will fail when computing
h
if m1 < m2
.
This can be easily fixed by computing
h = qInv(m1 + p - m2) mod por, alternatively, as we do it in our BigDigits implementation of RSA,
if (bdCompare(m1, m2) < 0)
bdAdd(m1, m1, p);
bdSubtract(m1, m1, m2);
/* Let h = qInv ( m_1 - m_2 ) mod p. */
bdModMult(h, qInv, m1, p);
A very simple example of RSA encryption
This is an extremely simple example using numbers you can work out on a pocket calculator (those of you over the age of 35 45 can probably even do it by hand).- Select primes p=11, q=3.
- n = pq = 11.3 = 33
phi = (p-1)(q-1) = 10.2 = 20 - Choose e=3
Check gcd(e, p-1) = gcd(3, 10) = 1 (i.e. 3 and 10 have no common factors except 1),
and check gcd(e, q-1) = gcd(3, 2) = 1
therefore gcd(e, phi) = gcd(e, (p-1)(q-1)) = gcd(3, 20) = 1 - Compute d such that ed ≡ 1 (mod phi)
i.e. compute d = e-1 mod phi = 3-1 mod 20
i.e. find a value for d such that phi divides (ed-1)
i.e. find d such that 20 divides 3d-1.
Simple testing (d = 1, 2, ...) gives d = 7
Check: ed-1 = 3.7 - 1 = 20, which is divisible by phi. - Public key = (n, e) = (33, 3)
Private key = (n, d) = (33, 7).
c = me mod n = 73 mod 33 = 343 mod 33 = 13.
Hence the ciphertext c = 13.
To check decryption we compute
m' = cd mod n = 137 mod 33 = 7.
Note that we don't have to calculate the full value of 13 to the power 7 here. We can make use of the fact that
a = bc mod n = (b mod n).(c mod n) mod n
so we can break down a potentially large number into its components and combine the results of easier, smaller calculations to calculate the final value.
One way of calculating m' is as follows:-
Note that any number can be expressed as a sum of powers of 2. So first compute values of 132, 134, 138, ... by repeatedly squaring successive values modulo 33.
132 = 169 ≡ 4, 134 = 4.4 = 16, 138 = 16.16 = 256 ≡ 25.
Then, since 7 = 4 + 2 + 1, we have m' = 137 = 13(4+2+1) = 134.132.131
≡ 16 x 4 x 13 = 832 ≡ 7 mod 33
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 32Note 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, 1 and n-1 will always do this for any n, no matter how large. But in practice, these shouldn't be a problem when we use large values for n in the order of several hundred bits.
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 me 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.
A slightly less simple example of the RSA algorithm
[Updated 25 November 2012] 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. Please note that this method is not secure in any way. It just shows another example of the mechanism of RSA with small numbers.For this example, to keep things simple, we'll limit our characters to the letters A to Z and the space character.
ATTACK AT SEVEN = ATT ACK _AT _SE VENIn the same way that any decimal number can be represented uniquely as the sum of powers of ten, e.g.
135 = 1 x 102 + 3 x 101 + 5,
we can represent our blocks of three characters as the sum of powers of 27 using SPACE=0, A=1, B=2, C=3, .. E=5, .. K=11, .. N=14, .. S=19, T=20, .. V=22, ..., Z=26.
ATT ⇒ 1 x 272 + 20 x 271 + 20 = 1289
ACK ⇒ 1 x 272 + 3 x 271 + 11 = 821
_AT ⇒ 0 x 272 + 1 x 271 + 20 = 47
_SE ⇒ 0 x 272 + 19 x 271 + 5 = 518
VEN ⇒ 22 x 272 + 5 x 271 + 14 = 16187
Using this system of integer representation, the maximum value of a block (ZZZ) is 273-1 = 19682, so we require
a modulus n greater than this value.
ACK ⇒ 1 x 272 + 3 x 271 + 11 = 821
_AT ⇒ 0 x 272 + 1 x 271 + 20 = 47
_SE ⇒ 0 x 272 + 19 x 271 + 5 = 518
VEN ⇒ 22 x 272 + 5 x 271 + 14 = 16187
- We choose e = 3
- We select primes p=173 and q=149 and check
- gcd(e, p-1) = gcd(3, 172) = 1 ⇒ OK
- gcd(e, q-1) = gcd(3, 148) = 1 ⇒ OK.
- Thus we have n = pq = 173 × 149 = 25777, and
phi = (p-1)(q-1) = 172 × 148 = 25456. - We compute d = e-1 mod phi = 3-1 mod 25456 = 16971.
- Note that ed = 3 × 16971 = 50913 = 2 × 25456 + 1
- That is, ed ≡ 1 mod 25456 ≡ 1 mod phi
- Hence our public key is (n, e) = (25777, 3) and our private key is (n, d) = (25777, 16971). We keep the values of p, q, d and phi secret.
c = me mod n = 12893 mod 25777 = 18524.
Overall, our plaintext
ATTACK AT SEVEN
is represented by the sequence of five integers
m1, m2, m3, m4, m5:
m_i = (1289, 821, 47, 518, 16187)We compute corresponding ciphertext integers ci = mie mod n, (which is still possible by using a calculator, honest):
c1 = 12893 mod 25777 = 18524
c2 = 8213 mod 25777 = 7025
c3 = 473 mod 25777 = 715
c4 = 5183 mod 25777 = 2248
c5 = 161873 mod 25777 = 24465
We can send this sequence of integers, ci, to the person who has the private key.
c2 = 8213 mod 25777 = 7025
c3 = 473 mod 25777 = 715
c4 = 5183 mod 25777 = 2248
c5 = 161873 mod 25777 = 24465
c_i = (18524, 7025, 715, 2248, 24465)We can compute the inverse of these ciphertext integers using m = cd mod n to verify that the RSA algorithm still holds. However, this is now outside the realm of hand calculations.
To help you carry out these modular arithmetic calculations, download our free modular arithmetic command line programs.
For example, to compute
1852416971 mod 25777
, use the bd_modexp
command:
bd_modexp 18524 16971 25777 18524^16971 mod 25777 = 1289
You should get the results:
m1 = 1852416971 mod 25777 = 1289
m2 = 702516971 mod 25777 = 821
m3 = 71516971 mod 25777 = 47
m4 = 224816971 mod 25777 = 518
m5 = 2446516971 mod 25777 = 16187
m2 = 702516971 mod 25777 = 821
m3 = 71516971 mod 25777 = 47
m4 = 224816971 mod 25777 = 518
m5 = 2446516971 mod 25777 = 16187
To convert these integers back to the block of three letters, do the following. For example, given m = 16187,
Similarly, m = 47 is encoded as follows:
16187 ÷ 272 = 16187 ÷ 729 = 22 rem 149, 22 → 'V'
149 ÷ 271 = 149 ÷ 27 = 5 rem 14, 5 → 'E'
14 ÷ 270 = 14 ÷ 1 = 14 rem 0, 14 → 'N'
Hence the integer m = 16187 represents the string "VEN".
149 ÷ 271 = 149 ÷ 27 = 5 rem 14, 5 → 'E'
14 ÷ 270 = 14 ÷ 1 = 14 rem 0, 14 → 'N'
47 ÷ 272 = 0 rem 47, 0 → SPACE;
47 ÷ 271 = 1 rem 20, 1 → 'A';
20 ÷ 270 = 20 rem 0, 20 → 'T'
giving the string "_AT".
Question: Why can't we use this method of encoding integers into blocks of three letters to encode the ciphertext?
A caution about this example
Note that this example is a very insecure method of encryption and should not be used in practice. We can easily factorize the modulus and hence break the cipher.Factorising a small RSA modulus
Starting with the knowledge that the modulus 25777 is the product of exactly two distinct prime numbers, and that one of these must be less than its integer square root (why?), a little testing of suitable candidates from a table of prime numbers will get you the answer pretty quickly.
Given n = 25777, compute √25777 = 160.55, and then work downwards
through the prime numbers < 160, i.e. (157, 151, 149, 139, ...),
and try to divide into n in turn:
157: 25777 / 157 = 164 remainder 29, so not a factor;
151: 25777 / 151 = 170 remainder 107, so not a factor;
149: 25777 / 149 = 173 exactly, so we have it.
You could also write a simple computer program to factor n that just divides it by
every odd number starting from 3 until it either finds an exact factor or stops when it reaches a number greater than the square
root of n.
157: 25777 / 157 = 164 remainder 29, so not a factor;
151: 25777 / 151 = 170 remainder 107, so not a factor;
149: 25777 / 149 = 173 exactly, so we have it.
A real example
In practice, we use a modulus of size in the order of 1024 bits. That is a number over 300 decimal digits long. One example isn = 11929413484016950905552721133125564964460656966152763801206748195494305685115033 38063159570377156202973050001186287708466899691128922122454571180605749959895170 80042105263427376322274266393116193517839570773505632231596681121927337473973220 312512599061231322250945506260066557538238517575390621262940383913963This is composed of the two primes
p = 10933766183632575817611517034730668287155799984632223454138745671121273456287670 008290843302875521274970245314593222946129064538358581018615539828479146469 q = 10910616967349110231723734078614922645337060882141748968209834225138976011179993 394299810159736904468554021708289824396553412180514827996444845438176099727With a number this large, we can encode all the information we need in one big integer. We put our message into an octet string and then convert to a large integer.
Also, rather than trying to represent the plaintext as an integer directly, we generate a random session key and use that to encrypt the plaintext with a conventional, much faster symmetrical algorithm like Triple DES or AES-128. We then use the much slower public key encryption algorithm to encrypt just the session key.
The sender A then transmits a message to the recipient B in a format something like this:-
Session key encrypted with RSA = xxxx
Plaintext encrypted with session key = xxxxxxxxxxxxxxxxx
The recipient B would extract the encrypted session key and use his private key (n,d) to
decrypt it. He would then use this session key with a conventional symmetrical decryption
algorithm to decrypt the actual message.
Typically the transmission would include in plaintext details of the encryption algorithms used,
padding and encoding methods, initialisation vectors and other details required by the recipient.
The only secret required to be kept, as always, should be the private key.
Plaintext encrypted with session key = xxxxxxxxxxxxxxxxx
If Mallory intercepts the transmission, he can either try and crack the conventionally-encrypted plaintext directly, or he can try and decrypt the encryped session key and then use that in turn. Obviously, this system is as strong as its weakest link.
When signing, it is usual to use RSA to sign the message digest of the message rather than the message itself. A one-way hash function like SHA-1 or SHA-256 is used. The sender A then sends the signed message to B in a format like this
Hash algorithm = hh
Message content = xxxxxxxxx...xxx
Signature = digest signed with RSA = xxxx
The recipient will decrypt the signature to extract the signed message digest, m;
independently compute the message digest, m',
of the actual message content; and check that m and m' are equal.
Putting the message digest algorithm at the beginning of the message enables the recipient to
compute the message digest on the fly while reading the message.
Message content = xxxxxxxxx...xxx
Signature = digest signed with RSA = xxxx
Thank you so much for all the wonderful information about this topic! I love your work
ReplyDeleteDigital Marketing Courses in Bangalore
Digital Marketing Training in Bangalore
Data Science Courses in Bangalore
Java Training in Bangalore
Data Science Training in Bangalore
Best Java Training Institutes in Bangalore
Best Seo Training in Bangalore
Best AWS Training in Bangalore
Devops Course in Bangalore
Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information.
ReplyDeleteIt's helpful for me my friend. Also great blog here with all of the valuable information you have.
AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery
SASVBA provides professional Artificial intelligence Institute in Delhi with the help of industry experts. Artificial intelligence is a method of building a computer-controlled robot, computer, or software that thinks wisely as well as intelligently. It is science and technology-based on subjects such as computer science, biology, psychology, linguistics, mathematics, and engineering.
ReplyDeleteSASVBA is recognized as the best machine learning training in Delhi. Whether you are a project manager, college student, or IT student, Professionals are the best machine learning institute in Delhi, providing the best learning environment, experienced machine learning instructors, and flexible training programs for the entire module.
ReplyDeleteMachine Learning Training in Delhi
SASVBA Provides the Best Data analytics course in Delhi with the Latest Development Environment and Structures. We keep Our Programs Up to Date with the Latest modern trends. SASVBA Is One of the best education Data Analytics courses in Delhi Which Helps Learners Crack Interviews in Tech Giants.
ReplyDeleteData Analytics Course in Delhi
SASVBA Delhi offers comprehensive Excel and Advanced Excel Training in Delhi. Extensive hands-on labs delivered by Excel and the Advanced Excel Institute in Delhi include live projects and simulations. Such detailed Excel and Advanced Excel courses have helped our students to get jobs in various multinational companies
ReplyDeleteAdvanced Excel course in Delhi
SASVBA offers one of the deepest SAS Training in Delhi. SAS certified students can quickly apply for jobs at MNC. Participants must enroll in the best SAS training institute in Delhi to acquire technology-related skills. Our main strength is our trainers, who are seasoned professionals and experienced in their industry.
ReplyDeleteSAS Course in Delhi
When it comes to repairing or servicing mobile devices, you can count on us as professionals. Whatever the problem with your mobile phone, we are here to solve it. We have a team of certified technicians who can help you if your phone's screen is damaged or the touch screen is not working.
ReplyDeleteMobile Repair in Delhi
Good article. Did you encounter that site you want to access is getting blocked, and you want to get into it? then you can check our article Access Blocked Content is generally accessible across various stores, including Mac OS and Windows application stores.
ReplyDelete