Encryption Basics

6+ hours across 72 lessons — Data Structures, Algorithms, Cryptography, Binary, Software Design, and Essential Unix Skills.
If you've ever dabbled with cryptography you've probably done the basics - something like a Caesar cipher - where you increment the letters of the alphabet by some value. We won't spend too much time doing that, nor will we dive into the various encryption algorithms out there. Instead, we'll come to understand that protecting your key is the name of the game when it comes to secure cryptography.
To that end, we'll create a one-time pad to see how disposable keys can help accomplish this. We'll then create our very own Diffie-Hellman key exchange to see how this groundbreaking algorithm changed cryptography forever.
The Code
Here's the code used in the video. There's always room for improvement - feel free to suggest! Your comments, as always, are welcome and you can drop me an email or, when the code is published, feel free to leave an issue on GitHub.
This is our alphabet for the one-time pad:
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Alphabet</span> {
<span class="hljs-title function_">constructor</span>(<span class="hljs-params"></span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span> = [
<span class="hljs-string">"A"</span>, <span class="hljs-string">"B"</span>, <span class="hljs-string">"C"</span>, <span class="hljs-string">"D"</span>,
<span class="hljs-string">"E"</span>, <span class="hljs-string">"F"</span>, <span class="hljs-string">"G"</span>, <span class="hljs-string">"H"</span>,
<span class="hljs-string">"I"</span>, <span class="hljs-string">"J"</span>, <span class="hljs-string">"K"</span>, <span class="hljs-string">"L"</span>,
<span class="hljs-string">"M"</span>, <span class="hljs-string">"N"</span>, <span class="hljs-string">"O"</span>, <span class="hljs-string">"P"</span>,
<span class="hljs-string">"Q"</span>, <span class="hljs-string">"R"</span>, <span class="hljs-string">"S"</span>, <span class="hljs-string">"T"</span>,
<span class="hljs-string">"U"</span>, <span class="hljs-string">"V"</span>, <span class="hljs-string">"W"</span>, <span class="hljs-string">"X"</span>,
<span class="hljs-string">"Y"</span>, <span class="hljs-string">"Z"</span>, <span class="hljs-string">" "</span>, <span class="hljs-string">"?"</span>, <span class="hljs-string">"!"</span>
];
}
<span class="hljs-title function_">getOffsetChar</span>(<span class="hljs-params">{char, offsetChar, encrypting=<span class="hljs-literal">true</span>}</span>){
<span class="hljs-keyword">const</span> charPosition = <span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span>.<span class="hljs-title function_">indexOf</span>(char);
<span class="hljs-keyword">const</span> keyPosition = <span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span>.<span class="hljs-title function_">indexOf</span>(offsetChar);
<span class="hljs-keyword">const</span> offsetPosition = encrypting ? (charPosition + keyPosition) : (charPosition - keyPosition);
<span class="hljs-keyword">let</span> idx = (offsetPosition) % <span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span>.<span class="hljs-property">length</span>;
<span class="hljs-keyword">if</span>(offsetPosition < <span class="hljs-number">0</span> ) idx = <span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span>.<span class="hljs-property">length</span> - -idx;
<span class="hljs-keyword">return</span> <span class="hljs-variable language_">this</span>.<span class="hljs-property">chars</span>[idx]
}
}
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Alphabet</span>();
Here's super-simple Caesar cipher:
<span class="hljs-keyword">const</span> <span class="hljs-title function_">offsetChars</span> = (<span class="hljs-params">text, offset=<span class="hljs-number">1</span></span>) => {
<span class="hljs-keyword">const</span> out = [];
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>; i < text.<span class="hljs-property">length</span>; i++){
<span class="hljs-keyword">const</span> offsetIndex = text.<span class="hljs-title function_">charCodeAt</span>(i) + offset;
out.<span class="hljs-title function_">push</span>(<span class="hljs-title class_">String</span>.<span class="hljs-title function_">fromCharCode</span>(offsetIndex))
}
<span class="hljs-keyword">return</span> out.<span class="hljs-title function_">join</span>(<span class="hljs-string">""</span>);
}
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Caesar</span> {
<span class="hljs-title function_">constructor</span>(<span class="hljs-params">offset=<span class="hljs-number">5</span></span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">offset</span>=offset;
}
<span class="hljs-title function_">encrypt</span>(<span class="hljs-params">plainText</span>){
<span class="hljs-keyword">return</span> <span class="hljs-title function_">offsetChars</span>(plainText, <span class="hljs-variable language_">this</span>.<span class="hljs-property">offset</span>);
}
<span class="hljs-title function_">decrypt</span>(<span class="hljs-params">cipherText</span>){
<span class="hljs-keyword">return</span> <span class="hljs-title function_">offsetChars</span>(cipherText, -<span class="hljs-variable language_">this</span>.<span class="hljs-property">offset</span>);
}
}
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-title class_">Caesar</span>;
The famous one-time pad that is theoretically unbreakable:
<span class="hljs-keyword">const</span> alphabet = <span class="hljs-built_in">require</span>(<span class="hljs-string">"./alphabet"</span>);
<span class="hljs-keyword">const</span> pads = <span class="hljs-built_in">require</span>(<span class="hljs-string">"../data/pads"</span>);
<span class="hljs-keyword">const</span> offsets = <span class="hljs-keyword">function</span>(<span class="hljs-params">text, pad, encrypting=<span class="hljs-literal">true</span></span>){
<span class="hljs-keyword">const</span> out = [], chars=text.<span class="hljs-title function_">split</span>(<span class="hljs-string">""</span>), keys=pad.<span class="hljs-title function_">split</span>(<span class="hljs-string">""</span>);
<span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>; i < text.<span class="hljs-property">length</span>; i++){
<span class="hljs-keyword">const</span> offsetChar = alphabet.<span class="hljs-title function_">getOffsetChar</span>({<span class="hljs-attr">char</span>: chars[i], <span class="hljs-attr">offsetChar</span>: keys[i], <span class="hljs-attr">encrypting</span>: encrypting})
out.<span class="hljs-title function_">push</span>(offsetChar)
}
<span class="hljs-keyword">return</span> out.<span class="hljs-title function_">join</span>(<span class="hljs-string">""</span>);
}
<span class="hljs-keyword">class</span> <span class="hljs-title class_">OneTimePad</span>{
<span class="hljs-title function_">constructor</span>(<span class="hljs-params">padNumber</span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">pad</span> = pads[padNumber];
<span class="hljs-variable language_">this</span>.<span class="hljs-property">alphabetLength</span> = alphabet.<span class="hljs-property">length</span>;
}
<span class="hljs-title function_">encrypt</span>(<span class="hljs-params">plainText</span>){
<span class="hljs-keyword">return</span> <span class="hljs-title function_">offsets</span>(plainText,<span class="hljs-variable language_">this</span>.<span class="hljs-property">pad</span>,<span class="hljs-literal">true</span>);
}
<span class="hljs-title function_">decrypt</span>(<span class="hljs-params">cipherText</span>){
<span class="hljs-keyword">return</span> <span class="hljs-title function_">offsets</span>(cipherText,<span class="hljs-variable language_">this</span>.<span class="hljs-property">pad</span>,<span class="hljs-literal">false</span>);
}
}
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-title class_">OneTimePad</span>;
Finally, the Diffie-Hellman key exchange:
<span class="hljs-keyword">class</span> <span class="hljs-title class_">DiffieHellman</span>{
<span class="hljs-title function_">constructor</span>(<span class="hljs-params">{generator, secret, modulus}={}</span>){
<span class="hljs-variable language_">this</span>.<span class="hljs-property">generator</span> = generator;
<span class="hljs-variable language_">this</span>.<span class="hljs-property">secret</span> = secret;
<span class="hljs-variable language_">this</span>.<span class="hljs-property">modulus</span> = modulus;
<span class="hljs-variable language_">this</span>.<span class="hljs-property">publicKey</span> = <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">generator</span>,<span class="hljs-variable language_">this</span>.<span class="hljs-property">secret</span>) % <span class="hljs-variable language_">this</span>.<span class="hljs-property">modulus</span>;
}
<span class="hljs-title function_">encryptionKey</span>(<span class="hljs-params">receiverPublicKey</span>){
<span class="hljs-keyword">return</span> <span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(receiverPublicKey, <span class="hljs-variable language_">this</span>.<span class="hljs-property">secret</span>) % <span class="hljs-variable language_">this</span>.<span class="hljs-property">modulus</span>;
}
}
<span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = <span class="hljs-title class_">DiffieHellman</span>;
I'll have this code up on GitHub when the videos are completed.
6+ hours across 72 lessons — Data Structures, Algorithms, Cryptography, Binary, Software Design, and Essential Unix Skills.