Add tests for encrypting and decrypting.

This commit is contained in:
Robbie Antenesse 2018-02-20 22:53:33 -07:00
parent 8dc15d27f5
commit a8bc9e7555
3 changed files with 61 additions and 2 deletions

40
index.html Normal file
View File

@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>One-Time Pad Generator</title>
<meta name="description" content="One-Time Pad Generator">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<strong>Your Message</strong><br>
<textarea id="input"></textarea><br>
<button id="encryptInput">Encrypt</button><br>
<br>
<strong>One-Time Pad</strong><br>
<pre id="pad"></pre><br>
<strong>Encrypted Message</strong><br>
<pre id="encrypted"></pre><br>
<br>
<hr>
<br>
<strong>Their Message</strong><br>
<textarea id="encryptedInput"></textarea><br>
<strong>One-Time Pad</strong><br>
<textarea id="encryptedInputPad"></textarea><br>
<button id="decryptInput">Decrypt</button><br>
<br>
<strong>Decrypted Message</strong><br>
<pre id="decrypted"></pre><br>
<script src="./index.js"></script>
</body>
</html>

View File

@ -8,13 +8,14 @@ function generatePad(string) {
const pad = [];
for (let i = 0; i < string.length; i++) {
const letter = Math.floor(Math.random() * CHARS.length);
pad.push(letter);
pad.push(CHARS[letter]);
}
return pad;
}
function encrypt(string) {
const strippedString = string.replace(/[^A-Z0-9]/g, '');
const strippedString = string.replace(/[^a-zA-Z0-9]/g, '');
console.log('strippedString', strippedString);
const pad = generatePad(strippedString);
return {
oneTimePad: pad,
@ -33,3 +34,17 @@ function decrypt(string, pad) {
return CHARS[(letterValue - padValue) % CHARS.length];
}).join('');
}
document.getElementById('encryptInput').onclick = () => {
const input = document.getElementById('input').value;
const encryption = encrypt(input);
document.getElementById('pad').innerHTML = encryption.oneTimePad.join('');
document.getElementById('encrypted').innerHTML = encryption.encryptedMessage;
}
document.getElementById('decryptInput').onclick = () => {
const input = document.getElementById('encryptedInput').value;
const pad = document.getElementById('encryptedInputPad').value.split('');
const output = decrypt(input, pad);
document.getElementById('decrypted').innerHTML = output;
}

View File

@ -4,6 +4,10 @@
"description": "Make secure communication easy and fun!",
"author": "Robbie Antenesse <dev@alamantus.com>",
"license": "MIT",
"scripts": {
"dev": "parcel ./index.html",
"build": "parcel build ./index.html"
},
"devDependencies": {
"babel-preset-env": "^1.6.1",
"parcel-bundler": "^1.6.2"