Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is the entire source code for the article “Subtitution ciphering in JavaScript”
- * http://timseverien.com/articles/153-substitution-ciphering-in-javascript/
- */
- (function() {
- "use strict";
- /**
- * Cipher namespace, to store all the cyphers
- *
- * @namespace
- */
- var Cipher = {};
- /**
- * Maps ABCDEF to QWERTY
- *
- * @param {string} text
- * @param {boolean} decode
- *
- * @return {string}
- */
- Cipher.toQWERTY = function(text, decode) {
- // ABCDEF to QWERTY map
- var map = {
- a: '01 ', b: '02 ', c: '03 ',
- d: '04 ', e: '05 ', f: '06 ',
- g: '07 ', h: '08 ', i: '09 ',
- j: '10 ', k: '11 ', l: '12 ',
- m: '13 ', n: '14 ', o: '15 ',
- p: '16 ', q: '17 ', r: '18 ',
- s: '19 ', t: '20 ', u: '21 ',
- v: '22 ', w: '23 ', x: '24 ',
- y: '25 ', z: '26 ', " ": '00 ', _: '00 ',
- 1: '34 ', 2: '35 ', 3: '36' ,
- 4: '37 ', 5: '38 ', 6: '39' ,
- 7: '40 ', 8: '41 ', 9: '42' ,
- ",": '27 ', ".": '28 ', "!": '29 ',
- "?": '30 ', ":": '33 ',
- };
- // Flip the map
- if(decode) {
- map = (function() {
- var tmp = {};
- var k;
- // Populate the tmp variable
- for(k in map) {
- if(!map.hasOwnProperty(k)) continue;
- tmp[map[k]] = k;
- }
- return tmp;
- })();
- }
- return text.split('').filter(function(v) {
- // Filter out characters that are not in our list
- return map.hasOwnProperty(v.toLowerCase());
- }).map(function(v) {
- // Replace old character by new one
- // And make it uppercase to make it look even fancier
- return map[v.toLowerCase()].toUpperCase();
- }).join('');
- };
- window.Cipher = Cipher;
- })();
- // OOOOOOOOOOYYYYYYYYYY
- var _text = "DETECTIVE:Correct. "
- var _text = _text.substring(0, 20);
- var _text2 = Cipher.toQWERTY(_text)
- window.prompt(""," " + _text2 + "\n # " + _text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement