A Demonstration of RSA Encryption

This example now uses a shortcut to large exponentiations described by Dr. Herong Yang.

by Vinyasi

HTML

<div align="center">
<h1>
A Demonstration of RSA Math

<br />

In terms of the DNA Code:

<br />

<small><small>
<i>
guanine, adenine, thymine and cytosine.
</i>
</small></small>
</h1>

<h3 id="charSet"></h3>
<h3 id="inputText"></h3>
<h3 id="outputMyAscii"></h3>
<h3 id="outputCodedMessage"></h3>
<h3 id="outputDecodedMyAscii"></h3>
<h3 id="outputDecodedMessage"></h3>
</div>

<p id="outputEncodingProcedure"></p>

<p id="outputDecodingProcedure"></p>

<br />

<p>
Compare the procedures, above, with what the <a href="https://web.archive.org/web/20071101161340/https://www.rsa.com/rsalabs/node.asp?id=2214">RSA company says...</a>
</p>

<blockquote><hr>
The RSA algorithm works as follows: take two large primes, p and q, and compute their product n = pq; n is called the modulus. Choose a number, e, less than n and <i>relatively prime</i><b>[coprime]</b> to [<b>&phi;</b>](p-1)(q-1), which means e and [<b>&phi;</b>](p-1)(q-1) have no common factors except 1. Find another number d such that (ed - 1) is divisible by [<b>&phi;</b>](p-1)(q-1). <b>[e &times; d &equiv; 1 mod &phi;]</b> The values e and d are called the public and private exponents, respectively. The public key is the pair (n, e); the private key is (n, d). The factors p and q may be destroyed or kept with the private key.
<hr></blockquote>

<br />

<p>
Also, refer to: <a href="http://people.csail.mit.edu/rivest/Rsapaper.pdf">http://people.csail.mit.edu/rivest/Rsapaper.pdf</a>
</p>

<br />

<p>
Credit for learning how to do this goes to <a href="https://www.youtube.com/channel/UCCh8eOn7IubOKnw_TMS-25A">Jordan Haack</a> on YouTube: <a href="https://www.youtube.com/watch?v=e42kE9XIK7g"><i>RSA Encyption</i></a> (this is exactly how he spelled it). This exercise focuses on the math behind RSA encryption by limiting the alphabet's character set to a mere three to prevent JavaScript from causing errors.
</p>

<br />

<div align="center">
RSA works, because of: <a...

CSS

P {
  text-align: justify;
  text-indent: 50px;
  width: 50%;
  margin: auto;
  border: 3px none;
}
BLOCKQUOTE {
  text-align: justify;
  text-indent: 50px;
  width: 47%;
  margin: auto;
  border: 3px none;
}
#centeredimg {
    display: block;
    margin-left: auto;
    margin-right: auto;
    border-width: none;
    width: 100%;
    }
A:active {text-decoration: none;}
A:hover {text-decoration: none; background-color: yellow;}

JavaScript

var shiftBy = 2;
//var alphabet = ['o', '!', 'W'];
var alphabet = ['G', 'A', 'T', 'C'];
var alphabetLength = alphabet.length;

// Write the character set: the linguistic alphabet used for this example...
document.getElementById("charSet").innerHTML = '<i>The Alphabet for this Exercise is Limited<br />to a Set of ' + alphabetLength + ' Characters for Simplicity:</i> <br /><span style="color: magenta;">' + alphabet + '</span>';

// 'message' is the message to undergo encryption...
//var message = "WoW!";
var message = "GATTACA";
var messageExtras;
var linkText = 'gattaca';
if (message.toLowerCase() == linkText)
{
	messageExtras = '<a href="http://w.gattaca.us/">' + message + '</a>';
}
else
{
	messageExtras = message;
}
var messageLength = message.length;

var i = 0;
var temp;
var myAscii = [];

for (i = 0; i < messageLength; i++)
{
	temp = message.charAt(i);
	myAscii[i] = alphabet.indexOf(temp) + shiftBy;
}

// Write the initial message and its myAscii equivalent...
document.getElementById("inputText").innerHTML = '<i>Original Message:</i> <br /><span style="color: blue;">' + messageExtras + '</span>';
document.getElementById("outputMyAscii").innerHTML = '<i>Converted to myAscii Values:</i> <br /><span style="color: orange;">' + myAscii + '</span>';

// p and q are two primes such that...
// p times q must be greater than 5 since I'm splitting
// the coded character string into individual characters
// whose greatest myAscii value will be less than 6
var p =7;
var q = 5;

var n = p * q; // 35
var phi = (p - 1) * (q - 1); // 24

// thanks to Wolfram Alpha...
// https://www.wolframalpha.com/input/?i=factor+24
// the prime factorization of phi, namely 24, is:
// 2^3 x 3  (4 prime factors, 2 distinct)

// 'e' must not be a factor of 'phi'
// gcd(e, phi) = 1
// Hence...
// gcd(29, 24) = 1
// https://www.wolframalpha.com/input/?i=gcd(29,+24)
// and must be less than 'n'
// Hence...
// e < n --> 29 < 35
var e = 29;

// Break down the exponentiation of a base into simpler...