zimmermanntel

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

ztel.js (3105B)


      1 String.prototype.replaceAll = function(search, replacement) {
      2     return this.replace(new RegExp(search, 'g'), replacement);
      3 };
      4 
      5 String.prototype.shuffle = function () {
      6     var a = this.split(""),
      7         n = a.length;
      8 
      9     for(var i = n - 1; i > 0; i--) {
     10         var j = Math.floor(Math.random() * (i + 1));
     11         var tmp = a[i];
     12         a[i] = a[j];
     13         a[j] = tmp;
     14     }
     15     return a.join("");
     16 }
     17 
     18 var alphabetNumbers = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
     19 
     20 var axis = generateAxis();
     21 var grid = generateGrid();
     22 
     23 function generateGrid() {
     24     return alphabetNumbers.shuffle();
     25 }
     26 
     27 function generateAxis() {
     28     return alphabetNumbers.shuffle().substring(0, 6);
     29 }
     30 
     31 function encrypt(axises, zimmermannGrid, message) {
     32     grid = zimmermannGrid.toUpperCase();
     33     axis = axises.toUpperCase();
     34     var encryptedMessage = "";
     35 
     36     if(grid.length !== 36) {
     37         return "Sorry, but your message could not be encrypted because the " +
     38         "grid length was not thirty-six characters.";
     39     }
     40     if(axis.length !== 6) {
     41         return "Sorry, but your message could not be encrypted because the " +
     42         "axis value was not six characters.";
     43     }
     44 
     45     message = message.replaceAll(" ", "").replace(/\W/g, "").toUpperCase();
     46 
     47     for(i = 0; i < message.length; i++) {
     48         for(j = 0; j < 36; j++) {
     49             if(grid.charAt((Math.floor(j / 6) * 6) + (j % 6))
     50                 === message.charAt(i)) {
     51                     encryptedMessage += axis.charAt(Math.floor(j / 6))
     52                         + axis.charAt(j % 6);
     53             }
     54         }
     55     }
     56 
     57     var tempMessage = encryptedMessage;
     58     return encryptedMessage;
     59 }
     60 
     61 function decrypt(axises, zimmermannGrid, message) {
     62     grid = zimmermannGrid.toUpperCase();
     63     axis = axises.toUpperCase();
     64 
     65     if(grid.length !== 36) {
     66         return "Sorry, but your message could not be decrypted because the " +
     67         "grid length was not thirty-six characters.";
     68     }
     69     if(axis.length !== 6) {
     70         return "Sorry, but your message could not be decrypted because the " +
     71         "axis value was not six characters.";
     72     }
     73 
     74     message = message.replaceAll(" ", "").toUpperCase().replace(/\W/g, "");
     75 
     76     var decryptedMessage = "";
     77     for(i = 0; i < message.length; i = i + 2) {
     78         for(j = 0; j < 36; j++) {
     79             if("" + axis.charAt(Math.floor(j / 6)) + axis.charAt(j -
     80                 (Math.floor(j / 6) * 6)) ===
     81                     message.substring(i, i + 2).toUpperCase()) {
     82                         decryptedMessage += grid.substring(j, j + 1);
     83             }
     84         }
     85     }
     86     return decryptedMessage;
     87 }
     88 
     89 function encFromDoc() {
     90     document.getElementById("outputDisplay").innerHTML =
     91         encrypt(document.getElementById("axisVals").value,
     92             document.getElementById("gridVals").value,
     93                 document.getElementById("userIn").value);
     94 }
     95 
     96 function decFromDoc() {
     97     document.getElementById("outputDisplay").innerHTML =
     98         decrypt(document.getElementById("axisVals").value,
     99             document.getElementById("gridVals").value,
    100                 document.getElementById("userIn").value);
    101 }