Skip to content

Instantly share code, notes, and snippets.

@rheajt
Last active November 21, 2016 20:38
Show Gist options
  • Save rheajt/d97477b54b7e2e7479bbf4a5c1852d31 to your computer and use it in GitHub Desktop.
Save rheajt/d97477b54b7e2e7479bbf4a5c1852d31 to your computer and use it in GitHub Desktop.

Create a crypto gadget

function doGet() {
//display the cryto.html page
return HtmlService.createHtmlOutputFromFile('Crypto');
}
<label>Crypto-Key Value</label>
<input type="number" id="crypto-key" value="5">
<label>Type the code you want encrypted here</label>
<textarea id="crypto"></textarea>
<h1 id="crypto-display"></h1>
<script>
//empty string to put the crypto into as it is generated
var cryptoCode = '';
document.getElementById('crypto-key').addEventListener('change', function() {
//if the crypto-key is changed, reset all the inputs
cryptoCode = '';
document.getElementById('crypto-display').innerHTML = '';
document.getElementById('crypto').value = '';
});
document.getElementById('crypto').addEventListener('keyup', function(e) {
//get the value of the key
var cryptoKey = document.getElementById('crypto-key').value;
//create an array of alphabet characters
var alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
//get the location fo the letter in the array and add the value of the cryptoKey
var keyLoc = alphabet.indexOf(e.key.toUpperCase()) + +cryptoKey;
//handle the key taking the value over the length of the alpahbet
if(keyLoc < 26) {
cryptoCode += alphabet[keyLoc];
} else {
cryptoCode += alphabet[keyLoc - 26];
}
//put the crypto into the html
document.getElementById('crypto-display').innerHTML = cryptoCode;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment