
Pete Jones
By: a guest on
Oct 18th, 2009 | syntax:
JavaScript | size: 1.75 KB | hits: 73 | expires: Never
var bifidEncode, bifidDecode;
// Assign closures that have access to the table
(function(){
// Generate the encryption table
/* * * * * * * * *
* 1 2 3 4 5 6 *
* 1 A B C D E F *
* 2 G H I J K L *
* 3 M N O P Q R *
* 4 S T U V W X *
* 5 Y Z 0 1 2 3 *
* 6 4 5 6 7 8 9 *
* * * * * * * * */
var _table = (function(){
var result = {};
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (var i = 0; i < chars.length; i += 1) {
var c = chars.charAt(i);
var index = Math.floor( i / 6 ).toString() + ( i % 6 ).toString();
result[c] = index;
result[index] = c;
}
return result;
})();
bifidEncode = function( rawText ) {
var rawIndices = "";
for( var i = 0; i < rawText.length; i++ ) {
rawIndices += _table[rawText.charAt( i )];
}
// cipherIndices = rawIndices[::2] + rawIndices[1::2]
var cipherIndices = "";
for( var i = 0; i < rawIndices.length; i += 2 ) {
cipherIndices += rawIndices.charAt( i );
}
for( var i = 1; i < rawIndices.length; i += 2 ) {
cipherIndices += rawIndices.charAt( i );
}
var cipherText = "";
for( var i = 0; i < cipherIndices.length; i += 2 ) {
cipherText += _table[cipherIndices.substr( i, 2 )];
}
return cipherText;
};
bifidDecode = function( cipherText ) {
var cipherIndices = "";
for( var i = 0; i < cipherText.length; i++ ) {
cipherIndices += _table[cipherText.charAt( i )];
}
var rawIndices = "";
for( var i = 0; i < cipherText.length; i++ ) {
rawIndices += cipherIndices[i];
rawIndices += cipherIndices[i + cipherText.length];
}
var rawText = "";
for( var i = 0; i < rawIndices.length; i += 2 ) {
rawText += _table[rawIndices.substr( i, 2 )];
}
return rawText;
};
})();