zimmermanntel

zimmermann telegram cryptography
git clone git://git.benoneill.xyz/zimmermanntel
Log | Files | Refs | LICENSE

commit 6233133e5f93d4db4e7a4e67091d9c328d6f5968
Author: Benjamin O'Neill <benjaminmoneill@gmail.com>
Date:   Fri, 26 May 2017 21:33:57 -0400

Modified license

Diffstat:
Aexample.txt | 8++++++++
Aindex.html | 47+++++++++++++++++++++++++++++++++++++++++++++++
Alicense.txt | 7+++++++
Astyle.css | 0
Aztel.js | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/example.txt b/example.txt @@ -0,0 +1,8 @@ +Grid: +B2E5RLI9NA1C3D4F6G7H8J0KMOPQSTUVWXYZ + +Message: +FGAFAAAVXADGAVXVADADDVDDDVGAVXVDXDVDDFAFDXGXGDDGAVFDVXVAAFXGDADXVDDXDAVXXVAAAVDAVXDAVVGDDXAVDGDXGXVXVDVFVVAFDXAVAFVXDXVDFDAFXAVVVFAVAFVVVVVADGXVAXAFDGGXFXAFAVVADGDFVFAXVDVXXFDAVXGDVAAFXGDADXVDVFAVAFVFDGAVAFVXVDAXAFDGXDAFAFVAAADGVVVVXVVDDFVVGDVDAVVXDFVDVXDADXAFAAAFAVDFVVVXVDAVFGFGXFDGVVGDDADFFXVXVDDFFDDX + +Axis: +ADFGVX diff --git a/index.html b/index.html @@ -0,0 +1,47 @@ +<html> + <head> + <title>Zimmermann Telegram</title> + <link rel="stylesheet" type="text/css" href="style.css" /> + </head> + + <body> + <div id="header"> + <h4> + Zimmermann Telegram Cryptography. Created by Benjamin O'Neill + </h4> + </div> + + <div id="wrapper"> + <p>Axis values:</p> + <input type="text" id="axisVals" /> + <br /> + <br /> + <p>Grid values:</p> + <input type="text" id="gridVals" /> + <br /> + <br /> + <p>Message to encrypt/decrypt:</p> + <input type="text" id="userIn" /> + <br /> + <br /> + <button id="encButton" onclick="encFromDoc()">Encrypt</button> + <button id="decButton" onclick="decFromDoc()">Decrypt</button> + <br /> + <br /> + <br /> + <p>Output:</p> + <br /> + <p id="outputDisplay"></p> + </div> + + <div id="footer"> + <h4> + Licensed under MIT License. + Check license file <a href="license.txt">here</a>. + </h4> + </div> + + <script src="ztel.js"></script> + </body> + +</html> diff --git a/license.txt b/license.txt @@ -0,0 +1,7 @@ +Copyright 2017 Benjamin O'Neill + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/style.css b/style.css diff --git a/ztel.js b/ztel.js @@ -0,0 +1,101 @@ +String.prototype.replaceAll = function(search, replacement) { + return this.replace(new RegExp(search, 'g'), replacement); +}; + +String.prototype.shuffle = function () { + var a = this.split(""), + n = a.length; + + for(var i = n - 1; i > 0; i--) { + var j = Math.floor(Math.random() * (i + 1)); + var tmp = a[i]; + a[i] = a[j]; + a[j] = tmp; + } + return a.join(""); +} + +var alphabetNumbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +var axis = generateAxis(); +var grid = generateGrid(); + +function generateGrid() { + return alphabetNumbers.shuffle(); +} + +function generateAxis() { + return alphabetNumbers.shuffle().substring(0, 6); +} + +function encrypt(axises, zimmermannGrid, message) { + grid = zimmermannGrid.toUpperCase(); + axis = axises.toUpperCase(); + var encryptedMessage = ""; + + if(grid.length !== 36) { + return "Sorry, but your message could not be encrypted because the " + + "grid length was not thirty-six characters."; + } + if(axis.length !== 6) { + return "Sorry, but your message could not be encrypted because the " + + "axis value was not six characters."; + } + + message = message.replaceAll(" ", "").replace(/\W/g, "").toUpperCase(); + + for(i = 0; i < message.length; i++) { + for(j = 0; j < 36; j++) { + if(grid.charAt((Math.floor(j / 6) * 6) + (j % 6)) + === message.charAt(i)) { + encryptedMessage += axis.charAt(Math.floor(j / 6)) + + axis.charAt(j % 6); + } + } + } + + var tempMessage = encryptedMessage; + return encryptedMessage; +} + +function decrypt(axises, zimmermannGrid, message) { + grid = zimmermannGrid.toUpperCase(); + axis = axises.toUpperCase(); + + if(grid.length !== 36) { + return "Sorry, but your message could not be decrypted because the " + + "grid length was not thirty-six characters."; + } + if(axis.length !== 6) { + return "Sorry, but your message could not be decrypted because the " + + "axis value was not six characters."; + } + + message = message.replaceAll(" ", "").toUpperCase().replace(/\W/g, ""); + + var decryptedMessage = ""; + for(i = 0; i < message.length; i = i + 2) { + for(j = 0; j < 36; j++) { + if("" + axis.charAt(Math.floor(j / 6)) + axis.charAt(j - + (Math.floor(j / 6) * 6)) === + message.substring(i, i + 2).toUpperCase()) { + decryptedMessage += grid.substring(j, j + 1); + } + } + } + return decryptedMessage; +} + +function encFromDoc() { + document.getElementById("outputDisplay").innerHTML = + encrypt(document.getElementById("axisVals").value, + document.getElementById("gridVals").value, + document.getElementById("userIn").value); +} + +function decFromDoc() { + document.getElementById("outputDisplay").innerHTML = + decrypt(document.getElementById("axisVals").value, + document.getElementById("gridVals").value, + document.getElementById("userIn").value); +}