Advertisement
Guest User

TableFile

a guest
Nov 1st, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. * This is the entire source code for the article “Subtitution ciphering in JavaScript”
  4.  
  5. * http://timseverien.com/articles/153-substitution-ciphering-in-javascript/
  6.  
  7. */
  8.  
  9.  
  10.  
  11. (function() {
  12.  
  13.     "use strict";
  14.  
  15.  
  16.  
  17.     /**
  18.  
  19.      * Cipher namespace, to store all the cyphers
  20.  
  21.      *
  22.  
  23.      * @namespace
  24.  
  25.      */
  26.  
  27.  
  28.  
  29.     var Cipher = {};
  30.  
  31.  
  32.  
  33.     /**
  34.  
  35.      * Maps ABCDEF to QWERTY
  36.  
  37.      *
  38.  
  39.      * @param {string} text
  40.  
  41.      * @param {boolean} decode
  42.  
  43.      *
  44.  
  45.      * @return {string}
  46.  
  47.      */
  48.  
  49.  
  50.  
  51.     Cipher.toQWERTY = function(text, decode) {
  52.  
  53.         // ABCDEF to QWERTY map
  54.  
  55.         var map = {
  56.  
  57.             a: '01 ', b: '02 ', c: '03 ',
  58.  
  59.             d: '04 ', e: '05 ', f: '06 ',
  60.  
  61.             g: '07 ', h: '08 ', i: '09 ',
  62.  
  63.             j: '10 ', k: '11 ', l: '12 ',
  64.  
  65.             m: '13 ', n: '14 ', o: '15 ',
  66.  
  67.             p: '16 ', q: '17 ', r: '18 ',
  68.  
  69.             s: '19 ', t: '20 ', u: '21 ',
  70.  
  71.             v: '22 ', w: '23 ', x: '24 ',
  72.  
  73.             y: '25 ', z: '26 ', " ": '00 ', _: '00 ',
  74.  
  75.             1: '34 ', 2: '35 ', 3: '36' ,
  76.  
  77.             4: '37 ', 5: '38 ', 6: '39' ,
  78.  
  79.             7: '40 ', 8: '41 ', 9: '42' ,
  80.  
  81.             ",": '27 ', ".": '28 ', "!": '29 ',
  82.  
  83.             "?": '30 ', ":": '33 ',
  84.  
  85.         };
  86.  
  87.  
  88.  
  89.         // Flip the map
  90.  
  91.         if(decode) {
  92.  
  93.             map = (function() {
  94.  
  95.                 var tmp = {};
  96.  
  97.                 var k;
  98.  
  99.  
  100.  
  101.                 // Populate the tmp variable
  102.  
  103.                 for(k in map) {
  104.  
  105.                     if(!map.hasOwnProperty(k)) continue;
  106.  
  107.                     tmp[map[k]] = k;
  108.  
  109.                 }
  110.  
  111.  
  112.  
  113.                 return tmp;
  114.  
  115.             })();
  116.  
  117.         }
  118.  
  119.  
  120.  
  121.         return text.split('').filter(function(v) {
  122.  
  123.             // Filter out characters that are not in our list
  124.  
  125.           return map.hasOwnProperty(v.toLowerCase());
  126.  
  127.         }).map(function(v) {
  128.  
  129.            // Replace old character by new one
  130.  
  131.             // And make it uppercase to make it look even fancier
  132.  
  133.             return map[v.toLowerCase()].toUpperCase();
  134.  
  135.         }).join('');
  136.  
  137.     };
  138.  
  139.  
  140.  
  141.  
  142.  
  143.     window.Cipher = Cipher;
  144.  
  145. })();
  146.  
  147. //           OOOOOOOOOOYYYYYYYYYY
  148. var _text = "DETECTIVE:Correct.  "
  149.  
  150.  
  151.  
  152. var _text = _text.substring(0, 20);
  153.  
  154. var _text2 = Cipher.toQWERTY(_text)
  155.  
  156.  
  157.  
  158. window.prompt("","    " + _text2 + "\n    # " +  _text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement