Advertisement
Guest User

Pete Jones

a guest
Oct 18th, 2009
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var bifidEncode, bifidDecode;
  2. // Assign closures that have access to the table
  3. (function(){
  4.     // Generate the encryption table
  5.     /* * * * * * * * *
  6.      *   1 2 3 4 5 6 *
  7.      * 1 A B C D E F *
  8.      * 2 G H I J K L *
  9.      * 3 M N O P Q R *
  10.      * 4 S T U V W X *
  11.      * 5 Y Z 0 1 2 3 *
  12.      * 6 4 5 6 7 8 9 *
  13.      * * * * * * * * */
  14.     var _table = (function(){
  15.         var result = {};
  16.         var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  17.         for (var i = 0; i < chars.length; i += 1) {
  18.             var c         = chars.charAt(i);
  19.             var index     = Math.floor( i / 6 ).toString() + ( i % 6 ).toString();
  20.             result[c]     = index;
  21.             result[index] = c;
  22.         }
  23.         return result;
  24.     })();
  25.    
  26.     bifidEncode = function( rawText ) {
  27.         var rawIndices = "";
  28.         for( var i = 0; i < rawText.length; i++ ) {
  29.             rawIndices += _table[rawText.charAt( i )];
  30.         }
  31.         // cipherIndices = rawIndices[::2] + rawIndices[1::2]
  32.         var cipherIndices = "";
  33.         for( var i = 0; i < rawIndices.length; i += 2 ) {
  34.             cipherIndices += rawIndices.charAt( i );
  35.         }
  36.         for( var i = 1; i < rawIndices.length; i += 2 ) {
  37.             cipherIndices += rawIndices.charAt( i );
  38.         }
  39.         var cipherText = "";
  40.         for( var i = 0; i < cipherIndices.length; i += 2 ) {
  41.             cipherText += _table[cipherIndices.substr( i, 2 )];
  42.         }
  43.         return cipherText;
  44.     };
  45.    
  46.     bifidDecode = function( cipherText ) {
  47.         var cipherIndices = "";
  48.         for( var i = 0; i < cipherText.length; i++ ) {
  49.             cipherIndices += _table[cipherText.charAt( i )];
  50.         }
  51.         var rawIndices = "";
  52.         for( var i = 0; i < cipherText.length; i++ ) {
  53.             rawIndices += cipherIndices[i];
  54.             rawIndices += cipherIndices[i + cipherText.length];
  55.         }
  56.         var rawText = "";
  57.         for( var i = 0; i < rawIndices.length; i += 2 ) {
  58.             rawText += _table[rawIndices.substr( i, 2 )];
  59.         }
  60.         return rawText;
  61.     };
  62. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement