Skip to content

Instantly share code, notes, and snippets.

@elisaado
Created October 17, 2024 08:24
Show Gist options
  • Save elisaado/d16912aab5281b7a6075d5ecdb9b409a to your computer and use it in GitHub Desktop.
Save elisaado/d16912aab5281b7a6075d5ecdb9b409a to your computer and use it in GitHub Desktop.
timeedit scrambling
"use strict";
/*jshint esversion: 8 */
/*jslint node: true */
//var TEScramble = (function () {
var my = {},
tabledata = [
["h=t&sid=", "6="],
["objects=", "1="],
["sid=", "2="],
["&ox=0&types=0&fe=0", "3=3"],
["&types=0&fe=0", "5=5"],
["&h=t&p=", "4="],
],
tabledataspecial = [
["=", "ZZZX1"],
["&", "ZZZX2"],
[",", "ZZZX3"],
[".", "ZZZX4"],
[" ", "ZZZX5"],
["-", "ZZZX6"],
["/", "ZZZX7"],
["%", "ZZZX8"],
],
pairs = [
["=", "Q"],
["&", "Z"],
[",", "X"],
[".", "Y"],
[" ", "V"],
["-", "W"],
],
pattern = [
4, 22, 5, 37, 26, 17, 33, 15, 39, 11, 45, 20, 2, 40, 19, 36, 28, 38, 30, 41,
44, 42, 7, 24, 14, 27, 35, 25, 12, 1, 43, 23, 6, 16, 3, 9, 47, 46, 48, 50,
21, 10, 49, 32, 18, 31, 29, 34, 13, 8,
];
function tablespecial(result) {
for (var i = 0; i < 100; i++) {
for (var index = 0; index < tabledataspecial.length; index++) {
var key = tabledataspecial[index];
result = result.replace(key[0], key[1]);
}
}
return result;
}
function tableshort(result) {
for (var index = 0; index < tabledata.length; index++) {
var key = tabledata[index];
result = result.replace(key[0], key[1]);
}
return result;
}
function modKey(ch) {
if (ch >= 97 && ch <= 122) {
return 97 + ((ch - 88) % 26);
}
if (ch >= 49 && ch <= 57) {
return 49 + ((ch - 45) % 9);
}
return ch;
}
function scrambleChar(ch) {
for (var index = 0; index < pairs.length; index++) {
var pair = pairs[index];
if (ch === pair[0]) {
return pair[1];
}
if (ch === pair[1]) {
return pair[0];
}
}
return String.fromCharCode(modKey(ch.charCodeAt(0)));
}
function swap(result, from, to) {
if (from < 0 || from >= result.length) {
return;
}
if (to < 0 || to >= result.length) {
return;
}
var fromChar = result[from];
result[from] = result[to];
result[to] = fromChar;
}
function swapPattern(result) {
// LMAO Note from Jetse: this should be `result.length / pattern.length` (division, not two params)
// TODO: check if ()/2 makes a difference
var steps = Math.ceil((result.length, pattern.length) / 2);
console.log(steps);
console.log(Math.ceil(result.length, pattern.length));
for (var step = 0; step < steps; step++) {
for (var index = 1; index < pattern.length; index += 2) {
swap(
result,
pattern[index] + step * pattern.length,
pattern[index - 1] + step * pattern.length
);
}
}
}
function swapChar(result) {
var split = result.split("");
for (index = 0; index < split.length; index++) {
split[index] = scrambleChar(split[index]);
}
swapPattern(split);
return split.join("");
}
function scramble(query) {
/* if (isEmpty(query)) {
return query;
}*/
if (query.length < 2) {
return query;
}
if (query.substring(0, 2) === "i=") {
return query;
}
var result = decodeURIComponent(query);
result = tableshort(result);
result = swapChar(result);
result = tablespecial(result);
return encodeURIComponent(result);
}
function applyTable(orig, table) {
return table.reduce((acc, [key, value]) => acc.replace(key, value), orig);
}
function unApplyTable(orig, table) {
return table.reduce((acc, [key, value]) => acc.replace(value, key), orig);
}
function unModKey(ch) {
if (ch >= 97 && ch <= 122) {
return 97 + ((ch - (97 - 26 + 9)) % 26);
}
if (ch >= 49 && ch <= 57) {
return 49 + ((ch - (49 - 9 + 4)) % 9);
}
return ch;
}
function unScrambleChar(ch) {
for (var index = 0; index < pairs.length; index++) {
var pair = pairs[index];
if (ch === pair[0]) {
return pair[1];
}
if (ch === pair[1]) {
return pair[0];
}
}
return String.fromCharCode(unModKey(ch.charCodeAt(0)));
}
const encode = (query) => {
const decoded = decodeURIComponent(query);
// const shortTabled = tableshort(decoded);
const shortTabled = applyTable(decoded, tabledata);
const split = shortTabled.split("").map((c) => scrambleChar(c));
for (let step = 0; step < split.length; step++) {
for (let index = 1; index < pattern.length; index += 2) {
swap(
split,
pattern[index] + step * pattern.length,
pattern[index - 1] + step * pattern.length
);
}
}
const swapped = split.join("");
const result = applyTable(swapped, tabledataspecial);
return result;
};
//////////////////////
const decode = (toDecode) => {
const unSpecialTabled = unApplyTable(toDecode, tabledataspecial);
const unSwapped = unSpecialTabled.split("");
for (
let step = 0;
step < Math.ceil(unSwapped.length / pattern.length);
step++
) {
for (let index = 1; index < pattern.length; index += 2) {
swap(
unSwapped,
pattern[index] + step * pattern.length,
pattern[index - 1] + step * pattern.length
);
}
}
const unCharScrambled = unSwapped.map((c) => unScrambleChar(c)).join("");
const unShortTabled = unApplyTable(unCharScrambled, tabledata);
return unShortTabled;
};
console.log(encode("h=t&sid=3&objects=7265.5"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment