Можно как-то так:
var Shuffler = (function() {
var joinReplaceCallback = function() {
return Math.random() > 0.5 ? 'e' : 'f';
};
return {
decode: function(array, key) {
var tempArray = array.slice(); //copy source array
array.splice(0, array.length); //clear source array
key = key.split(/[ef]/);
for(var i = 0, value; i !== key.length; i++) {
value = parseInt(key[i], 14);
array[value] = tempArray[i];
}
},
encode: function(array) {
var indexes = [],
key = [],
tempArray = array.slice(), //copy source array
value;
array.splice(0, array.length); //clear source array
while(array.length !== tempArray.length) {
value = Math.floor(Math.random() * tempArray.length);
if(indexes.indexOf(value) === -1) {
array.push(tempArray[value]);
indexes.push(value);
key.push(value.toString(14));
}
}
return key.join('$').replace(/\$/g, joinReplaceCallback);
}
};
})();
var array = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
var secretKey = Shuffler.encode(array);
console.log(array, secretKey);
Shuffler.decode(array, secretKey);
console.log(array);