A Demonstration of RSA Encryption
I don't know how to create a work around solution for JavaScript's inability at raising large or small numbers to large powers, so I limited myself to an alphabet of only three characters.
by Vinyasi
HTML
<div align="center">
<h1>
A Demonstration of RSA Math
</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>
<br />
<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>φ</b>](p-1)(q-1), which means e and [<b>φ</b>](p-1)(q-1) have no common factors except 1. Find another number d such that (ed - 1) is divisible by [<b>φ</b>](p-1)(q-1). <b>[e × d ≡ 1 mod φ]</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 />
<p>
Written by Vinyasi in the summer of 2016, copyleft.
</p>
<br /><br />
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;
}
A:active {text-decoration: none;}
A:hover {text-decoration: none; background-color: yellow;}
JavaScript
var shiftBy = 2;
var alphabet = ['o', '!', 'W'];
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<br />Limited 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 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;">' + message + '</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;
// solve e(d) = 1 (mod phi)
// solve 29(d) = 1 (mod 24)
// https://www.wolframalpha.com/input/?i=solve+29(d)+%3D+1+(mod+24)
var d = 5;
var encodingProcedure = 'Select two primes, <b>p</b> and <b>q</b>: <b>' + p + '</b> and <b>' + q + '</b>.';
encodingProcedure += '<br /><b>n</b> = p × q <—> ...