Prime Number Encryption Copy
Prime Number Encryption Copy
Mentor
Authors
Abstract
1
Contents
1 Introduction 4
2 Literature Review 6
3.1 Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Organisation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Results 11
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Proofs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3.3 Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.4 Decryption . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.5 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.6 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Conclusion 16
2
6 Evaluation 17
7 Glossary 19
8 References 20
3
1 Introduction
4
the ciphertext, Bob can apply his private key to recover the plaintext. Note
that they did not need to share keys in secret: Bob shared his public key with
everyone and his private key with no one. But how do we implement this
mechanism? That’s where prime numbers make their entrance to the art of
cryptography.
In 1978, Ron Rivest, Adi Shamir and Leonard Adleman published the RSA
algorithm, a pioneering public-key cryptosystem which generates strong keys
using a prime number trapdoor function [4]. (It is relatively easy for a com-
puter to quickly multiply two numbers, but relatively hard for a computer to
quickly factorise a number into its prime factors.) This makes it very easy to
encrypt messages but extremely difficult to decrypt messages without knowing
the private key. This method of encryption is used today in messaging apps
such as Telegram to encrypt messages that are sent between devices securely
and easily. RSA is also used to encrypt the keys for a symmetric-key cryptosys-
tem facilitating secure Internet connections. With RSA being so important to
cryptography and with technology at the center of our world, it is imperative
that the system remains secure. In this paper, we will explore how RSA works
in detail, analyse why it has worked so well for so long, and speculate how it
might be affected by the advent of quantum computers.
This paper provides a deep dive into the RSA cryptosystem — as well as pro-
viding our own implementation in Python — and discusses the future of the
technology in the age of quantum computing. We hope that this paper con-
tributes to the knowledge of the reader.
5
2 Literature Review
After learning why there is no perfect formula for primes, we decided to inves-
tigate how they can still be generated efficiently. We reviewed the proceedings
from the International Workshop on Cryptographic Hardware and Embedded
Systems [7]. In these proceedings, Joye et al. give methods of generating ran-
dom prime numbers efficiently and efficient ways of checking if numbers are
prime. More specifically, their research focuses on creating a new algorithm for
generating pseudo-random numbers with no small factors. The main limitation
of these proceedings was that the theories discussed may be too advanced, given
that less efficient but simpler ways to generate prime numbers are readily avail-
able. This research did not form the basis of our project, but it provided useful
insights into how to efficiently generate prime numbers.
6
RSA encryption and formed the basis of our research.
Before investigating the RSA algorithm, we read a paper titled New directions in
cryptography by Whitfield Diffie and Martin Hellman [3]. In this paper, Diffie
and Hellman explore the revolution in cryptography over the years. Firstly,
they define the best known cryptographic problem: privacy - “preventing the
unauthorized extraction of information from communications over an insecure
channel”. This problem can be solved by sending keys through a secure channel
and the main body of the message through the insecure channel. In further
sections they consider approaches to transmit key information through public
channels. The authors also discuss trapdoor functions, which are easy to com-
pute in one direction, yet difficult to compute in the opposite direction. Diffie
and Hellman emphasise throughout the paper that “secrecy is at the heart of
cryptography”. The article perfectly showcases the revolutionary perspective on
cryptography over the years alongside presenting various methods and imple-
mentations. Therefore this paper formed the basis of our research, as it deeply
explores the importance of cryptography as well as why it is widely used.
Next, we decided to analyse the original research paper written by the inventors
of the RSA algorithm: Ronald Rivest, Adi Shamir and Leonard Adleman [4].
In this article Rivest et al. describe a new cryptosystem that makes it possible
to share encryption keys without revealing the corresponding decryption keys.
The authors use a “trap-door one-way function” of prime numbers to eliminate
the need to transmit encryption keys through a secure messenger, instead al-
lowing them to be shared publicly. Their research focuses on finding such a
function, first proposed by Diffie and Hellman, to propose a ground-breaking
new cryptosystem [3]. This article was extremely useful to our research topic,
as we are researching prime numbers as well as the RSA algorithm. The main
limitation of the article was that it was one of the first public key cryptosystems,
thus the authors indicated that more research is needed to verify the proposed
security of such a system. (Indeed, in the 44 years since the article’s publication,
no known methods of defeating the system have been found, if a large enough
key is used.) This article formed the basis of our research; it has lots of useful
information on how prime numbers can be used to encrypt messages.
7
We have successfully examined the significance and main contribution of prime
numbers in prime number encryption [2], especially RSA algorithm which we
further researched in RSA paper [4]. Prime numbers are the fundamentals of all
numbers and cannot be further simplified unlike composite numbers. This was
proven to be beneficial as in case of any interception, the key will remain secure
as it cannot be easily derived from either public key or encrypted messages [8].
We also looked at generating random prime numbers for the RSA algorithm and
made a simple random prime number generator [7]. We have thoroughly looked
at how throughout time people have observed and tried to formulate a pattern
for the generation of primes [6].
8
3 Development and Methodology
3.1 Roles
Although we did not explicitly assign roles at the beginning of the project, each
member of our group ended up leading some aspect of the research.
• Andras organised the group’s meetings, set internal deadlines and co-wrote
the program. He also spent most time in perfecting the article and taking
care of any correspondence. He made sure to keep the group connected
on various collaborative platforms.
• Ataera researched the generation of large prime numbers, implemented
an efficient algorithm in Python, and did some research on the history of
RSA. He also read the Code Book, which contributed to this article.
• Karolina researched quantum computing to evaluate the effectiveness of
RSA encryption, including how Shor’s algorithm could be implemented
and corresponded with Dr Peter Shor. She also researched Elliptic Curve
Cryptography and the Riemann Hypothesis.
We met every Tuesday after our King’s Certificate lesson to plan tasks and
conduct research for one hour.
3.2 Organisation
We created a Gantt chart using Microsoft Excel to organise our work on this
project. A snippet of this chart is shown in Figure 1, while the full chart can
be found in Appendix B. We communicated with each other via a WhatsApp
group to ensure that we consistently met all of our deadlines.
9
Figure 1: A snippet of the Gantt chart used to organise our work on this project.
During our literature review, we learned about what prime numbers are, their
significance in encryption, and how to generate them efficiently [7]. Then we
focused on the RSA algorithm, investigating how prime numbers can be used
to create a one-way trapdoor function [4].
10
4 Results
4.1 Introduction
Our goal for this section was to demonstrate the effectiveness of the RSA algo-
rithm, and implement the encryption technique using prime numbers we have
generated in Python [4]. In order to do this, we first proved some useful the-
orems: Euclid’s Infinitive Prime Theorem, as well as Fermat’s Little Theorem.
Afterwards, we developed a program which generates random prime numbers
with a bit length specified by the number inputted. Once these primes are
generated, we can use them in our encryption program, which uses the RSA
encryption algorithm, to convert plaintext into ciphertext. Alongside the en-
cryption program, we have designed a corresponding decryption program which
will convert ciphertext back into plaintext.
4.2 Proofs
Consider a list of all prime numbers in increasing order, assuming that there is
a finite number of them.
p1 , p2 , p3 , . . . pn
P = p1 p2 p3 . . . pn
Let q = P + 1.
11
If q is prime, then it is also not in our list, as it is higher than the last prime
in our list. Therefore, it is shown that our list does not contain all the prime
numbers.
Fermat’s little theorem states that any number a raised to the power of any
prime p gives a remainder of a when divided by the prime p.
ap = a (mod p)
1p = 1 (mod p)
For the inductive hypothesis, assume that ap = a (mod p) for some integer a.
The goal is to show that (a + 1)p = a + 1 (mod p).
The left hand side can be expanded by using the binomial theorem:
p p p p−1 p p−2 p
(a + 1) = a + a + a + ··· + a+1
1 2 p−1
p!
We know that kp = k!(p−k)!
. We can see that p divides the numerator, i.e.
p | p!. We can also see thatp does not divide the denominator, i.e. p ∤ k! and
p ∤ (p − k)!. Therefore p | kp for 1 < k < p − 1.
So the terms in the middle are equal to 0 (mod p), reducing the left hand side
to
(a + 1)p = ap + 1 (mod p)
12
4.2.3 RSA Encryption
Given two prime numbers p and q, and a public key e co-prime to (p − 1)(q − 1),
the RSA algorithm computes the private key d such that ed ≡ 1 mod (p −
1)(q − 1). The product n = pq is also released with the public key.
To prove that m ≡ med mod pq, it is sufficient to prove that m ≡ med mod p
and m ≡ med mod q. If m − med is divisible by p and it is also divisible by q,
then it must be divisible by pq.
First, the function takes a number n which will form the length in bits for the
generated prime number. Then, it iterates through a loop checking whether
a randomly generated odd number is co-prime with the small prime numbers
up to 29, i.e. if it is not divisible by any of the first few prime numbers. If it
passes the verification, it moves on to the following branch and iterates through
dividing the large random number, “big” by all the prime numbers up to the
square root of the number. If it finds no factors of “big”, it returns it, and the
resulting number should be prime. If, however, it finds a factor, the loop exits
and tries a number which is greater than the previous value of “big” by 2. This
13
is because, the next possible prime number would be 2 spaces up, meaning it
would need to be odd, as even numbers share a common factor of 2.
To begin, the function generates two 48-bit prime numbers using the generate
prime function (see above), which are assigned to p and q. If p and q are equal,
then q is generated again to ensure that p and q are two unique prime numbers.
n is set to p ∗ q, and ϕ is set to (p − 1) ∗ (q − 1) which therefore makes ϕ co-prime
with n. It then generates a random number e, not higher than ϕ, that is co-
prime to ϕ. It then creates d through taking a modular multiplicative inverse of
e. This means choosing d such that ed ≡ 1 mod ϕ. The public key is (n, e) and
the private key is (n, d). The keys are now ready to be used in both encryption
and decryption for secure data transfer.
4.3.3 Encryption
4.3.4 Decryption
4.4 Analysis
The Python program is a demonstration of the RSA algorithm which utilises the
ideas used in Fermat’s little theorem and Euclid’s theorem of infinite primes to
generate large primes used for private and public keys. This then demonstrates
encryption and decryption using the RSA algorithm. The program can quickly
generate primes up to 48 bits long.
14
4.5 Evaluation
Our code has successfully shown the ability to generate large prime numbers
(to specified length of bits) which are then used to implement RSA encryption
[4]. In order to break RSA encryption; prime factors of a product number must
be found to derive the private key, and therefore obtain the original message.
This is however rather challenging if the product is significantly large, as it can
contain multiple pairs of prime factors which is incredibly time consuming thus
inefficient. Nevertheless, prime factorization could be improved in terms of time
using Shor’s algorithm - developed by Dr Peter Shor [9]. Shor’s algorithm trans-
forms an initial possible prime factor guess into a more reliable possible prime
factor. In conjunction with quantum computers, it contains enough power to
potentially break RSA. Therefore, it does raises concerns and further implica-
tions to our project. Nonetheless, as long as generated and implemented prime
numbers are relatively large, issues should be temporarily eliminated.
4.6 Conclusion
15
5 Conclusion
16
6 Evaluation
Table 1 shows a speed test of our final program. The bit length shows the
number of bits required to store each prime number. The average time shows the
how long the program took to run over 10 tests. The speed test was conducted
on an Apple M1 system on a chip with 8 GB RAM.
The main threat to the perfect security offered by RSA encryption is quantum
computers implementing Shor’s algorithm [9]. This algorithm, developed by
Dr Peter Shor in 1994, can be used to factor integers into their prime factor
components. This algorithm is highly reliable as it is probabilistic - results
have a high probability of success while probability of failure can be lowered by
repeating the algorithm. As a hybrid algorithm, it is separated into two parts:
reduction of the factoring problem into problem of order finding (can be done
on a classical computer), and solving the problem of order finding (algorithm
implemented on a quantum computer).
17
5. If r is odd, then repeat from step 1.
r
6. If k 2 = −1 mod N , then repeat from step 1.
r
7. The factors of N are gcd(k 2 ± 1, N ).
We emailed Dr Shor about the future of quantum computing and prime factori-
sation. Currently, quantum computers do not have enough qubits to factorise
large prime numbers, and thus break the RSA cryptosystem. This is because
“quantum gates are too noisy to run full scale fault tolerance on quantum com-
puters”. It is really hard to shield quantum gates from noise from the outside
world. However, the field of quantum computing is rapidly developing, and it
is “reasonably likely” that breakthroughs in the next 20 years could make it
possible to execute Shor’s Algorithm on quantum computers, although “it is
really hard to predict the progress of technology” [10].
y 2 = x3 + ax + b
Each x value has 2 corresponding y values which are symmetrical about the
x-axis, and any line will not intersect the curve in more than 3 points. To
begin encryption, a starting point A on a curve is picked. Then, tangents are
repeatedly drawn to find new points on the curve.
18
7 Glossary
Prime number A number with no factors other than one and itself.
19
8 References
[1] Knott R. The Development of Number Systems. Mathematics in
School. 1979;8(4):23-5. Available from: https://www.jstor.org/stable/
30213485.
[2] Singh S. The Code Book: The Secret History of Codes and Code-Breaking.
Fourth Estate; 1999.
20
Appendix A Python Program
21
Appendix B Gantt Chart
22
Appendix C RSA Algorithm Example
This is a worked example of the RSA algorithm at work with small primes.
We have now generated the public key (n, e) and the private key (n, d). At this
stage, p, q and ϕ are no longer needed and should be destroyed to eliminate
the risk of attackers using them to find the private key. The public key can be
released to the public, and the private key must be kept secret.
We can now use the public key to encrypt plaintext. We will encrypt the letter
H. First, we convert it to a number by finding its position in the alphabet. Our
plaintext becomes the number 8.
Next, we convert the number back into a letter by finding the 8th letter of the
alphabet. Our resulting plaintext is the letter H. We have successfully recovered
the original plaintext.
Note that the algorithm for encryption and decryption is very similar, with
exponents of e and d used respectively.
23