Skip to content

Instantly share code, notes, and snippets.

@notarikon-nz
Created August 27, 2024 06:53
Show Gist options
  • Select an option

  • Save notarikon-nz/d395729da7f950fa2850ff22f5f49f29 to your computer and use it in GitHub Desktop.

Select an option

Save notarikon-nz/d395729da7f950fa2850ff22f5f49f29 to your computer and use it in GitHub Desktop.
HackMUD deseancryption function (T3 decrypt)
function(context, args) // key:"iG1AmNA",str:"encryptedstring"
{
const HEADER = "\n`HAPERTURE SCIENCE :: DECRYPT LIBRARY`\n\n";
if (!args || !args.key || !args.str) return HEADER + `Error: Missing arguments - aperture.decrypt{key,str}\n`
// Key List
// Superceded in current gen by keygen
const knownKeys = "vW8=JHZ,nmV/OoZ,zo+SDcY,lxKTRA9,RuU4GxP,3Vp22ee,G7Cway3,evM5YiI,Kl2vQM0,QOt5Q5E,d+OWxih,VdKCr=x,fHeh8SB,hbAy0bO,oosFtfe,p7lVZN2,DU9ak4c,DdCEq=8,KcDX6gE,IsNSiCD,y5C1a7=,L1RlVtf,WRpktD0,SvhjUmQ,dBOS=J4,nS2llSK,cO1CUuR,zcjt+dN,q1lwA3+,zsDhpTq,WcwNaD0,UBZl7NH,uoOdpB7,6buX2xO,gtZykzT,pBx4RGZ,4WmCwiZ,xT99WoF,x+POwgU,3nCzypz,bLPLUEH,6TQ2xwr,EDDkm8e,PXZYMUo,+8va+4p,x6f4lSx,H4pNHWY,DghICo0,KIHV18m,V/YCU7T,CFbpmZ=,r4uLNU6,0d/9Ir=,B7M0KnI,2iXs7XW,WKo9PbL,jb1wvUI,Ahw1omr,rWSJSZu,zyBlW3h,WZj0tb=,LvtBVEH,3Uot1kv".split(",")
// Define the base64 characters
let base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// Define a regular expression to identify illegal characters
let regexIllegal = /[À–ºÍª¨õÚßúʶ—ç섹ك½ÄÞÂӀã¡äæáÒ»­¿ÇÌ󸤩£™Šþ•ïŒÝàÛù¢¸âåÅðË§¯Ô艳òü¥Ü²ë÷×ÿÁ÷û¬Õ˜‹ÏØýÑñ‚›’鮵П“îêö°”†ŽÖí¾Æ´«Î‘œž¼šô‡ˆ]/g;
// Extract the key from the arguments and convert it to indices in the base64 array
let key = args.key.split("").map(x => base64.indexOf(x));
// Array to store possible decrypted strings
let returnArray = [];
// Array to keep track of operations to discard
let discardArr = [];
// Array of key variations to try
let keyVariations = ["0",
"0246135",
"0123456601234556012344560123345601223456011234560",
"0123456123456023456013456012456012356012346012345",
"01234456011234556012234566012334560",
"012456123560234601345",
"012234456601123345560",
"01234601235601245601345602345612345"];
// Iterate over key variations
for (let i of keyVariations) {
let count = -1;
let string2 = [];
// Decrypt the string using the current key variation
let string = args.str.split("").map(x => base64.indexOf(x)).map(x => {
string2.push((x - key[i[++count % i.length]] + base64.length) % base64.length);
return (x + key[i[count % i.length]]) % base64.length;
}).map(x => base64[x]).join("");
// Decrypt the second part of the string
string2 = string2.map(x => base64[x]).join("");
// Test the decrypted strings
discardArr.push("decode");
testString(string, i);
discardArr.push("encode");
testString(string2, i);
// Break if a valid return array is found
if (returnArray.length > 0) break;
}
// Determine if the string was decoded or not
let decode;
if (discardArr.join("").includes("decodeencode")) decode = true;
else decode = false;
// Calculate the time taken for decryption
let ms = Date.now() - _START;
// Add index to discard array
let count = 0;
discardArr = discardArr.map(x => count++ + " " + x);
// Return decode status, time taken, return array, and discard array
return {
decode,
ms,
returnArray // ,discardArr
};
// Function to test decrypted strings
function testString(s, i) {
if ((s.match(/\+/g) || []).length / s.length > .10) {
returnArray.push({
s,
i,
t: "plain"
}); // plain text
} else if (/\/{6,}/.test(s)) {
returnArray.push({
s,
i,
t: "jpeg||png"
}); // jpeg or png file
} else {
s = decrypt64(s);
let illegalCount = (s.match(regexIllegal) || []).length;
let otherCount = (s.match(/[^\w\d-+_/?\[\](){}|\\<>]/g) || []).length;
if (/invitees/.test(s) || /\w{4,}.? \w{4,}/.test(s)) {
returnArray.push({
s,
i,
otherCount,
illegalCount,
len: s.length,
t: "b64"
}); //base64 converted
} else if (/\/{6,}/.test(s)) {
returnArray.push({
s,
i,
t: "b64:jpeg||png"
});
} else {
discardArr.push(s);
}
}
}
// Function to decrypt base64 string
function decrypt64(s) {
var e = {},
i, b = 0,
c, x, l = 0,
a,
r = '',
w = String.fromCharCode,
L = s.length,
A = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (i = 0; i < 64; i++) {
e[A.charAt(i)] = i;
}
for (x = 0; x < L; x++) {
c = e[s.charAt(x)];
b = (b << 6) + c;
l += 6;
while (l >= 8) {
((a = (b >>> (l -= 8)) & 0xff) || (x < (L - 2))) && (r += w(a));
}
}
return r;
}
// dtr's super tiny version
function d64(e,d) {
var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' // add = so it's b65, thanks sean
if(d)return d.replace(/=/g,'').split('').map(e => b64.indexOf(e).toString(2).padStart(6,0)).join('').match(/.{8}/g).map(e => String.fromCharCode(parseInt(e,2))).join('')
if(e)return e.split('').map(e => e.charCodeAt().toString(2).padStart(8,0)).join('').match(/.{0,6}/g).filter(e => e).map(e => b64[parseInt(e.padEnd(6,0).padStart(8,0),2)]).join('')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment