<!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"
    "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8"></meta>
<meta name="keywords"
content="RSA,code,Java,cyptography,algorithm,cryptographic,crypto,
secret,number theory"></meta>
<meta name="author"
content="Gihan Marasingha"></meta>
<title>The RSA Code and Congruences</title>

<style type="text/css">
div
{
	margin-top : 1em;
	margin-bottom : 1em;
}
div.abstract
{
	margin-left : 3em;
	margin-right : 3em;
	text-align : justify;
	font-style : italic;
}
div.navigation
{
	text-align : center;
}
p.java
{
  text-align : center;
}
h1 { font-variant : small-caps;}
body
{
  font-family : georgia,serif;
  margin-left : 2em;
  margin-right : 2em;
}
a{text-decoration : none;}
a:visited{ color : blue;}
a:hover{ color : green;}
a.listing:hover { font-style : italic; }

</style>
</head>

<body>

<hr />

<h1>The RSA 
Code and Congruences</h1>

<hr />

<p><strong>Note: This page requires a MathML-capable browser to
display properly.  If the symbol in brackets (<math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&longrightarrow;</mo>
</mrow></math>) isn't an arrow, please use the <a
href="index.html">HTML version</a> instead.</strong></p>

<div class="abstract">
<p>In this article, I discuss methods for encrypting
messages, with particular emphasis on the code known as RSA. The
background material on congruences, Euclid's algorithm, etc. is
also discussed. The material presented should by accessible to a
mathematics or theoretically-oriented science undergraduate, or
to a bright A-level student. Terms which may seem technical
appear in italics.</p></div>

<p></p>

<h2>Contents</h2>

<ol>
    <li><a class="listing" href="#introduction">Introduction</a></li>
    <li><a class="listing" href="#TheMathsBehindTheCode">The Maths behind the
        Code</a></li>
    <li><a class="listing" href="#ImplementingtheCode">Implementing the Code</a></li>
    <li><a class="listing" href="#javaimplementation">A Java Implementation of
        the RSA Code</a></li>
    <li><a class="listing" href="#congruences">Congruences and Euclid's Algorithm</a></li>
    <li><a class="listing" href="#NotesforNerds">Notes for Nerds (aka Technical
        Notes)</a></li>
    <li><a class="listing" href="#Links">Links</a></li>
    <li><a class="listing" href="#aboutdoc">About this Document</a></li>
</ol>

<h2>Java Applets</h2>

<ol>
    <li><a class="listing" href="#Gihanshcfcalculator">Hcf calculator</a></li>
    <li><a class="listing" href="#phicalculator">Phi calculator</a></li>
    <li><a class="listing" href="#factoriser">Factoriser</a></li>
    <li><a class="listing" href="#multiplier">Multiplier</a></li>
    <li><a class="listing" href="#javaimplementation">CodeIt</a></li>
</ol>

<p></p>

<h2><a id="introduction">Introduction</a></h2>

<p>The RSA code is an 'unbreakable' cipher - a means of
scrambling a message so that it will be indecipherable to prying
eyes. In mathematical terms, a cipher is a special kind of
mapping from an set A, called the <em>alphabet</em> (not
necessarily a natural alphabet!), to itself. The mapping must be
a <em>bijection</em>, i.e. it must be <em>invertible</em>,
because we would like to be able to recover the original message
given the encoded message.</p>

<p>One of the oldest ciphers was used by Julius Caesar and
involves permuting the letters of the alphabet, often simply shifting
each letter a fixed number of places. For instance, if we agreed to
shift every letter 3
places down, then the letter 'B' would become 'E', 'G' would
become 'J' and so on. The phrase 'fifth legion advance' would
become 'iliwk ohjlrq dgydqfh'. To get back the original message,
all we have to do is to transpose each letter 3 places up in the
alphabet. In a code such as the Caesarian, which is really a
collection of similar codes that depend on a parameter (in this
case the number of places by which each letter is shifted), the
parameter is called the <em>encryption key</em> </p>

<p>There are a number of problems with Caesar's method. Because
we are using the ordinary Roman alphabet, there are only
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mrow>
  <mn>26</mn>
  <mo> !</mo>
  <mo> = </mo>
  <mn>4.0329</mn>
  <mo> &times; </mo>
  <msup>
   <mn>10</mn>
   <mn>26</mn>
  </msup>
 </mrow>
</math>
different possible codes (the total number of <em>permutations</em>
on a set of size 26), this might seem like a big number, but
modern computers can search through all the possiblities in a
relatively small amount of time. (moreover, if we insist on using
simple shifts, then there are only 26 different possibilities,
one of which is clearly useless!) Another problem, of a more
practical nature, is how Caesar is going to let his generals know
of the encryption key.</p>

<p>The RSA code addresses both problems. On the one hand, it
allows the use of alphabets of arbitrary length, but, more
fundamentally, the main advantage of the RSA code is that even if
the encryption method should fall into enemy hands, it is still
very difficult to invert the cipher. For this reason, the RSA
code is sometimes known as public-key encryption, because the
encryption key can be divulged to everyone without posing a
security risk.</p>

<p></p>

<h2><a id="TheMathsBehindTheCode">The Maths behind the Code</a></h2>

<p>Before explaining how to encode a natural message, I will
discuss how the RSA code works on a special alphabet of numbers.
But we'll need to define a few mathematical terms along the way.</p>

<p><a id="Definitions"><strong>Definitions</strong></a><strong>
(see below for examples!)</strong></p>

<ol>
    <li>Let a and c be whole numbers. We say that c <em>divides</em>
        a if there exists a whole number d such that a = c d.</li>
    <li>A whole number p is said to be prime if the only divisors of p
        are 1 and p.</li>
    <li>Let a and b be whole numbers. The <em>highest common
        factor</em> of a and b ( written hcf(a,b) ) is any whole
        number, d , such that d divides a, d divides b; and if c
        is whole number such that c divides a and c divides b
        then c divides d. ( or, put another way, d is a 'common
        factor' of a and b, moreover, d is the 'highest' of the
        common factors. )</li>
    <li>Let a and n be whole numbers. We say that a is <em>coprime</em>
        to n ( or that a is <em>relatively prime</em> to n ) if
        hcf(a,n) = 1.</li>
    <li>Let n be a positve whole number. Define the set R(n) to
        be the whole numbers between 0 and n - 1 which are
        coprime to n. ( Note: this is non-standard notation )</li>
    <li>Let n be a positive whole number. We define the <em>Euler
        phi function</em>,
<math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n) = the
        size of R(n).</li>
</ol>

<p><strong>Examples</strong></p>

<ol>
    <li>5 divides 10. The number 2 divides any even number. Any
        whole number divides 0.</li>
    <li>The highest common factor of 24 and 16 is 4. hcf(10,7) =
        1. hcf(p,n) = 1 if p is a prime number and n is not a
        multiple of p. hcf(n,0) = n for any whole number n.</li>
    <li>10 is coprime to 7.</li>
    <li>If n = 10 then R(n) = {1, 3, 7, 9 }. If n = 21 then R(n)
        = { 1, 2, 4, 5, 8, 10, 11, 13, 16, 17, 19, 20 }. If n is
        a prime number then R(n) = { 1, 2, 3, 4, ..., n-1 }</li>
    <li><math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(21)
        = 12. If n is a prime number then <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n)
        = n-1.</li>
</ol>

<p>The following Java applet calculates the hcf of any two whole
numbers and expresses the hcf as a linear combination of the
numbers:<a id="Gihanshcfcalculator"></a></p>

<p class="java"><object classid="java:GetHcf.class" width="400" height="200"></object></p>

<p>Choose any (largish!) positive whole number n. <strong>The set
of elements which we will encode in the RSA scheme is simply R(n)</strong>.
This takes the place of the Roman alphabet in Caesar's code.</p>

<p>Look back at Caesar's code. What is really going on is that
there is a bijection f : A <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&longrightarrow;</mo></mrow></math> A (where
A is the Roman alphabet ) which tells us to shift each letter a
certain number of places. We are going to find a bijection f :
R(n) <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&longrightarrow;</mo></mrow></math> R(n) which is relatively easy
to carry out, but very hard to invert. This will be our code.</p>

<p>First we need a little theorem about <a href="#congruences">congruences</a>.
If you've never heard of these objects, follow the link in the
previous sentence.</p>

<p><strong>Lemma</strong></p>

<p>Let n be a positive whole number and t a whole number such
that t <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math> 1 mod <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n).
If x is coprime to n, then
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
  <msup>
    <mi>x</mi>
    <mi>t</mi>
  </msup>
  <mo>&Congruent;</mo>
  <mi>x</mi>
  <mi> mod </mi>
  <mi>n</mi>
  </mrow>
</math>
. The proof is
left as an easy exercise (easy iff you know the Fermat-Euler
theorem).</p>

<p><strong>Corollary</strong></p>

<p>If e is positive integer such that e is coprime to <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n) then the map f : R(n) <math
 xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&longrightarrow;</mo></mrow></math>R(n) 
 defined by
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
  <mrow>
    <mi>f</mi>
    <mo> ( </mo>
    <mi> x </mi>
    <mo> ) </mo>
  </mrow>
  <mo> = </mo>
  <mrow>
    <msup>
      <mi> x </mi>
      <mi> e </mi>
    </msup>
  </mrow>
</mrow>
</math>
is a bijection (i.e. is invertible).</p>  
<p><strong>Proof</strong></p>

<p>We'll use the above lemma to find an inverse. The idea is to
find a whole number d such that if t = ed then t <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>
1 mod <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n).
Then for any x in R(n),
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi> x </mi>
<mo> = </mo>
<msup>
  <mi> x </mi>
  <mi> t </mi>
</msup>
<mo> = </mo>
<msup>
  <mi> x </mi>
  <mrow>
      <mi> e </mi>
      <mi> d </mi>
  </mrow>
</msup>
<mo> = </mo>
<msup>
  <mrow>
   <mo>(</mo>
   <msup>
    <mi>x</mi>
    <mi>e</mi>
   </msup>
   <mo>)</mo>
  </mrow>
  <mi>d</mi>
 </msup>
</mrow>
</math>.
That is, the
mapping g : R(n) <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&longrightarrow;</mo></mrow></math>R(n)
defined by
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi> g </mi>
<mo> ( </mo>
<mi> y </mi>
<mo> ) </mo>
<mo> = </mo>
<msup>
  <mi> y </mi>
  <mi> d </mi>
</msup>
</mrow>
</math>,
is the required inverse for f. But such a d
<strong>does</strong> exist, because e is coprime to <math
xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n) (
see the section on <a href="#congruences">congruences</a> ). QED.</p>

<p></p>

<p>So now we have our bijection ( namely the function f
introduced in the corollary ). Why is it difficult to invert? If
we are to use the method of the corollary to find an inverse
function, we first need to calculate <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n).
The best method known for evaluating <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n)
requires factorising n ( i.e. writing n as a product of its
factors ). In general, for large n ( say 100 digits or more ),
this is a tough problem, particularly if n has only a small
number of factors. Often n is chosen to be the product of two
large prime numbers. Of course, there may be an alternative
method for finding the inverse function ( without needing to
calculate <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n) or to factorise n );
no-one knows of such a method, or if they do, they're keeping
very quiet about it!</p>

<p>If you would like to calculate <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(10) = 4. <math xmlns="http://www.w3.org/1998/Math/MathML">
 <mrow>
  <mi>&phi;</mi>
 </mrow>
</math>(n)
for yourself, the following fact is handy (no proof supplied!):</p>

<p><strong>Fact</strong></p>

<p>Let a, b be coprime positive whole numbers, then <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(a
b) = <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(a)
<math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(b).</p>
<p>Note: we must have that a and b are coprime because, for instance,
<math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(4)
= Size of {1,3} = 2. Whereas <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(2)
= Size of {1} = 1, and hence <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(4)
doesn't equal <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(2)
<math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(2).</p>
<p>Example: <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(21)
= <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(3
* 7) = <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(3)
* <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(7)
= 2 * 6 = 12.</p> <p>If you can't be bothered to calculate phi by
hand, I've written a phi calculator which will do it for you.</p> 
<p><strong>Warning!</strong> Calculating <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
involves factorising n, which generally takes a long time for large
n. (which is why the RSA code is so effective!), so if you enter a
large number ( say &gt;= 20 digits ), my programme may appear to
stall. Really, it's just thinking
very hard. :-(<a id="phicalculator"></a></p>

<p class="java"><object classid="java:Phi.class" width="400" height="200"></object></p>

<p></p>

<p>Another related programme, which you may find useful later on
is my factoriser:<a id="factoriser"></a></p>

<p class="java"><object classid="java:Factorise.class" width="400" height="200"></object></p>

<p>The following program multiplies any two integers
together (more useful than you might think!)<a id="multiplier"></a></p>

<p class="java"><object classid="java:Multiplier.class" width="400" height="200"></object></p>

<h2><a id="ImplementingtheCode">Implementing the Code</a></h2>

<p>It's all very well messing about with numbers, but what about
encrypting an actual message?!</p>

<p>One rather silly approach would be to assign each letter of
the alphabet to one of the elements of R(n). The drawback, as
with Caesar's method is that it effectively limits the number of
codes to only 26! (that's an exclamation point, and not the factorial
symbol.) So in order to increase the size of our
alphabet, we convert <em>blocks</em> of characters into integers.
The way we do this is first to assign an integer between 0 and
N-1 to each character (where there are a total of N characters in
our character set, e.g. N = 26 if we use the Roman alphabet as
our character set) then, having already chosen n, we define r to
the the unique positive integer such that
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup>
  <mi> N </mi>
  <mi> r </mi>
</msup>
<mo> &le; </mo>
<mi> n </mi>
<mo> &lt; </mo>
<msup>
  <mi> N </mi>
  <mrow>
  <mi> r </mi>
  <mo> + </mo>
  <mn> 1 </mn>
  </mrow>
</msup>
</mrow>
</math>.
Each block of r characters then corresponds to a base N
number which is between 0 and n-1 inclusive. <a
href="http://doesn'texist.link">When I get round to it, this will
be a link to a page about numbers in other bases.</a> This
number is encoded using the map
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi> x </mi>
<mo>&longrightarrow;</mo>
<msup>
  <mi> x </mi>
  <mi> e </mi>
</msup>
</mrow>
</math>.
Now
<math xmlns="http://www.w3.org/1998/Math/MathML">
<msup>
  <mi> x </mi>
  <mi> e </mi>
</msup>
</math>
is between 0 and n-1 (mod n), but not necessarily less
than
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup>
  <mi> N </mi>
  <mi> r </mi>
</msup>
</mrow>
</math>,
so it converts to a block of r+1 characters (not a block of
r characters). To decode, we simply invert the entire 
process. Note that we can only invert if x is coprime to n;
often, we will take n to be the product of two primes, say n = p
q, with p and q prime. Then the number of choices for x which are
coprime to n is <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
= <math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(p)<math
xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(q)
= (p-1)(q-1), which is close enough to n, for large p,q. If that's all
Greek to you, don't worry, because I have slaved away at my keyboard
and produced a Java applet which does the job for you:</p> <p></p> 

<h2><a id="javaimplementation">A Java Implemetation of the RSA
Code</a></h2>

<p>I have spent the last two weeks learning the
Java programming language just so that I could produce the
following applet. I've written it so
that you need to understand at least some of the maths in order
to be able to use it.</p>

<p>Just punch in your number n, and a number e if you wish to
encode and/or d if you wish to decode. Type your message in the
appropriate window (I'll let you figure out which one is the
appropriate window) and hit the encode or decode button. That's
it.</p>

<p>As far as I know, there aren't any bugs in the program, but
sometimes it will produce error messages. This is because <em>you</em>
did something wrong ;-)</p>

<table border="1" cellspacing="5">
    <tr>
        <th align="left">Error Message</th>
        <th align="left">Explanation</th>
    </tr>
    <tr>
        <td>java.lang.NumberFormatException: Zero length
        BigInteger</td>
        <td>You need to type in a number for n and for e or d</td>
    </tr>
    <tr>
        <td>java.lang.NumberFormatException: Choose n&gt;95</td>
        <td>Do as it tells you! I have chosen N = 95. If n &lt;=
        N, then the program won't be able to calculate r&gt;0
        such that
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup>
  <mi> N </mi>
  <mi> r </mi>
</msup>
<mo> &le; </mo>
<mi> n </mi>
<mo> &lt; </mo>
<msup>
  <mi> N </mi>
  <mrow>
  <mi> r </mi>
  <mo> + </mo>
  <mn> 1 </mn>
  </mrow>
</msup>
</mrow>
</math>.</td>
    </tr>
    <tr>
        <td>java.lang.NumberFormatException: 'x' isn't coprime to
        'n'</td>
        <td>This means that when one of the block of characters
        was converted to an integer x, x wasn't coprime to n. You
        can sometimes remedy this by simply adding a few extra
        characters at the beginning of your message. Otherwise,
        choose a decent n which has few factors!</td>
    </tr>
</table>

<p></p>

<p>If you're not sure how to get the software running, here's a
step-by-step list of what you have to do:</p>

<ol>
    <li>Choose an integer n such that n &gt;= 95. Preferably, n
        should have few factors.</li>
    <li>Figure out <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n).
        If you can         use my <a href="#phicalculator">phi
        calculator</a>, then         you've chosen a bad n, because
        anyone else can also use         the phi calculator to get
        <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n),
        and you're code can be cracked! Instead, use the
        multiplicativity of <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>.</li>
        <li>Choose an encoding key, e which is coprime to <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n).
        You can check that any         particular choice works by
        using the <a         href="#Gihanshcfcalculator">hcf
        calculator</a>. The         hcf of e and <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        should be 1.</li>     <li>Figure out the decoding key, d. You
        can do this using the         <a href="#Gihanshcfcalculator">hcf calculator</a>.         Type in <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        where the         programme asks you for a, and type in e
        where the program         asks you for b. The output should be
        something like: '         hcf is 1 = s <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        + t e'         (where s and t will be actual numbers). Just
        take your d         to be t. (so that d is the inverse of e
        mod <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n))</li>
        <li>If you want to encode a message, type n and e in the
        appropriate boxes and enter the message to be encoded in
        the window next to the 'encode' button. Hit the 'encode' 
        button and the encoded message will appear in the other
        window.</li>
    <li>If you want to encode a message, type n and d in the
        appropriate boxes and enter the message to be decoded in
        the window next to the 'decode' button. Hit the 'decode'
        button and the decoded message will appear in the other
        window.</li>
</ol>

<p><strong>Worked Example</strong></p>

<p><strong>N.B.</strong>.: Often you will need the output
from one of the programmes above in order to calculate n, e and
d. Note that with most windows operating systems, you can copy
and paste from the text boxes. e.g. with Microsoft Windows, just
select the text to be copied with your mouse, then hit Ctrl-C to
copy and Ctrl-V to paste. </p>

<ol>
    <li>First I mess around with the <a href="#factoriser">factoriser</a>
        to find two large primes, e.g. p = 5465464567 and q =
        6534535367. (In practise, you might like to try even
        larger primes, for extra security). We put n = pq.</li>
    <li>As p and q are coprime, <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        =         <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(p)<math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(q).
        Because, p and q are prime <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(p)
        = p-1 and <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(q)
        = q-1. so <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        = (p-1)(q-1) = 5465464566 *         6534535366 =
        35714271498146841156 (use my <a
        href="#multiplier">multiplier</a> to do the
        multiplication)</li>     <li>Pick an integer e, it will
        probably work! e.g. try e = 
        35. I check it works (i.e. is coprime to <math
        xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>&phi;</mi></mrow></math>(n)
        ) using the <a         href="#Gihanshcfcalculator">hcf
        calculator</a>. The         calculator tells me that the hcf
        of 35 and         35714271498146841156 is indeed 1.</li> 
    <li>The output from the hcf calculator is 'hcf is 1 =
        -16326524113438555957*35 + 16*35714271498146841156', so
        the decoding key, d, is -16326524113438555957</li>
    <li>To encode the string 'My name is Michel Cain', enter the
        data in the right places, then hit 'encode'. (note n = pq
        = 35714271510146841089). The encoded message is
        '(Im?*5AYe-&gt;X='|=tXL*wi%93@Ia!C' (without the
        apostrophes)</li>
    <li>I'll let you figure this step out yourself.</li>
</ol>

<p>If you want to use my programme to receive messages from other
people, you will typically tell them the numbers n and e, but
keep the number d a secret. They can then encode messages and
send them to you, but cannot decode anything!!. (Note that you
can copy the output of CodeIt and paste it into your mail
programme)</p>

<p class="java"><object classid="java:CodeIt.class" 
width="800" height="600"></object></p>

<p></p>

<h2><a id="congruences">Congruences and Euclid's Algorithm</a></h2>

<p>This section is for those who have not come across congruences
or Euclid's algorithm. If this applies to you, read on, else feel
free to skip it.</p>

<p>Congruences are a bit like equations, but different! We need
congruences to describe the RSA code, so I guess I'll have to
explain what they are ;-)</p>

<p><strong>Definition</strong></p>

<p>Let a, b, m be whole numbers. We say a is congruent b modulo m
and write 'a <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b mod m' if m divides
a-b.</p>

<p>For instance, 10 <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math> 4 mod 2, as 2
divides 6 = 10-4. Also 24 <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math> -1 mod 5,
as 5 divides 25 = 24-(-1).</p>

<p>Sometimes, the idea of congruence arithmetic is introduced by
looking at the arithmetic of clocks. Note that on a (twelve-hour)
clock, if you add 3 to 10, you get 1 ( because 3:00 + 10:00 =
13:00 = 1pm ). More generally, whenever you add on a multiple of
12 hours, you get back the same position of the hour hand. What
is really happening is that clock arithmetic is congruence
arithmetic modulo 12; we make precise the statement that 3 + 10
'=' 1 by observing that 3 + 10 = 13 <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>1
mod 12.</p>

<p>The reason we introduce congruences is that they behave in
many ways like ordinary equations. First, they have some nice
properties which enable you to combine congruences in simple ways
e.g. if a, b, c, m are whole numbers then:</p>

<ul>
    <li>a <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>a mod m</li>
    <li>if a <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    mod m then b <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>a
    mod m</li> 
    <li>if a <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    mod m and b <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>c
    mod m then a <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>c
    mod m</li> 
</ul>

<p>Also, like ordinary equations, one can add and multiply
congruences:</p>

<ul>
    <li>if a <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    mod m and a' <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b'
    mod m then a + a' <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    + b' mod m</li> 
    <li>if a <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    mod m and a' <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b'
    mod m then a * a' <math
    xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>b
    * b' mod m</li> 
</ul>

<p>All these statements require proof, of course, but I'll leave
them as exercises!</p>

<p>Next, we need something called Euclid's Algorithm in order to
be able to find congruence inverses. Suppose we have whole
numbers a and m such that a is coprime to m. We'd like to be able
to find a whole number b such that a b <math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&Congruent;</mo></mrow></math>1
mod m. If this were an ordinary equation then we'd just put b =
1/a, but of course, it isn't, and usually, 1/a won't even be a
whole number. Instead we use a nifty result which shows the
existence of whole numbers s and t, such that 1 = sa + tm. Then
we just take b = s and we're done.</p>

<p><strong>Lemma (Euclid's Algorithm)</strong></p>

<p>Let a, b be whole numbers and let d = hcf(a,b) ( follow this
link for a <a href="#Definitions">definition</a> ) then there
exist whole numbers s and t such that d = s a + t b.</p>

<p><strong>Proof</strong></p>

<p>It is a basic property of whole numbers that given a and b,
there exist whole numbers q and r such that a = bq + r and,
moreover, that 0&lt;= r &lt; |b|. One can check that hcf(a,b) =
hcf(b,r). Then, by induction on r, we have that there exist s'
and t' such that d = s' b + t' r. Consequently, d = s' b + t' (a
- bq) = t' a + (s' - t'q)b. So put s = t' and t = s' - t'q, then
d = sa + tb. QED.</p>

<p>Even if you don't get the proof, there's no need to worry as
I've written a Java applet which will do the calculations for
you. Click here for <a href="#Gihanshcfcalculator">Gihan's hcf
Calculator</a>.</p>

<p></p>

<h2><a id="NotesforNerds">Notes for Nerds (aka Technical
Notes)</a></h2>

<p>I am rather inexperienced with Java (at the date of writing,
I've only been at it two weeks), so my software may seem a little
rough round the edges. If you have any feedback on how to improve
the programmes, please <a href="mailto:marasing@maths.ox.ac.uk">email
me</a>.</p>

<p>Java is meant to be platform and country independent, but I
have sidestepped the latter feature by using the Unicode values
of the characters in 'CodeIt'. This will cause problems if your
operating system / web browser does not use the ASCII or
ISO8859-1 (Latin-1) document encoding method.</p>

<p>All the software on this page uses the java.math package.
(this package is new to Java 1.1, if you're browser doesn't
support Java 1.1, get one that does! See the <a
href="http://www.javasoft.com">JavaSoft</a> home page for more
details) The package java.math defines a class BigInteger which
supports arbitrary sized integers. This means that you can choose
'n' to be as large as you like in CodeIt.</p>

<p></p>

<h2><a id="Links">Links</a></h2>

<table border="0" cellpadding="5" cellspacing="4">
    <tr>
        <td><a href="http://www.rsa.com">RSA Data Security, Inc.</a></td>
        <td>This company was set up by the guys who invented the
        RSA code. Unsurprisingly, they specialise in cryptography
        methods.</td>
    </tr>
</table>


<h2><a id="aboutdoc">About this Document</a></h2>

<p>This document was written in XHTML and MathML.  To view it
properly, you need an up-to-date browser, such as <a
href="http://www.mozilla.org">mozilla</a>, and the required fonts.</p>

<p>On a modern Debian system, you should install the latex-xft-font debian
package, then copy the <a
href="http://support.wolfram.com/mathematica/systems/windows/general/latestfonts.html">Mathematica
4.1 fonts</a> to /usr/local/share/fonts/math-fonts. You may also need
to install the adobe symbol font as described in this document on <a
href="http://mcelrath.org/Notes/MathML">installing MathML on
linux</a>. Restart mozilla and you're up!</p>

<p>If you're running mozilla on Windows, follow the instructions on
the <a href="http://www.mozilla.org/projects/mathml/fonts/">mozilla
MathML webpage</a>.  You may need to save the file before running it
if installation throws up errors.
</p>

<p>This document is XHTML 1.1 compliant!</p>
<p>
      <a href="http://validator.w3.org/check?uri=referer"><img
          src="http://www.w3.org/Icons/valid-xhtml11"
          alt="Valid XHTML 1.1!" height="31" width="88" /></a>
</p>

<hr />

<div class="navigation">
<p><a href="../../index.html">Home</a></p>

<address>
<a href="mailto:marasing@maths.ox.ac.uk"><em>marasing@maths.ox.ac.uk</em></a>
</address>
<p><em>Last updated: 22/07/04</em></p>
</div>

<!-- Start of StatCounter Code -->
<script type="text/javascript">
var sc_project=570965; 
var sc_partition=4; 
var sc_security="12a09e4a"; 
var sc_invisible=1; 
</script>

<script type="text/javascript"
src="http://www.statcounter.com/counter/counter_xhtml.js"></script><noscript><div
class="statcounter"><a class="statcounter"
href="http://www.statcounter.com/"><img class="statcounter"
src="http://c5.statcounter.com/counter.php?sc_project=570965&amp;amp;java=0&amp;amp;security=12a09e4a&amp;amp;invisible=1"
alt="counter" /></a></div></noscript> 
<!-- End of StatCounter Code -->   

</body>
</html>
