We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

User:Aadenboy/Miscellanium 2's cipher

From Esolang
Jump to navigation Jump to search

The cipher used by Miscellanium 2 is based on two strings, where you require both strings and the deciphered string as it is currently known to decipher it. The format used is [ID]#[SUBID]; the names "ID" and "SUBID" are not relevant—all that is necessary is that ID defines the structure of the string and SUBID determines what is in it.

To encrypt with the cipher:

  • Initialize two empty strings ID and SUBID.
  • For each character in the string...
    • If this is the first occurrence of this character...
      • Append the character to SUBID.
      • Insert the index of the character in SUBID to ID. This will also be the first instance of that number in ID.
    • Otherwise...
      • Take the substring of characters between the current character and the previous instance of it.
      • Count how many unique characters there are in the substring.
      • Add the count to ID. This will never exceed the current length of SUBID.
hi im misc
1231421256#hi msc

To decrypt with the cipher:

  • Initialize an empty result string.
  • For each index in ID...
    • If this is the first occurrence of this index...
      • Add the character in SUBID with the same index to the result string.
    • Otherwise...
      • Pick the nth unique character starting from the end of the string, where n is the current index plus one.
      • Add the character at that final position to the result string.

For indexes greater than 9, they should be wrapped in parentheses, i.e 10 becomes (10).

Encryption implementations

Lua

o=io.read()u={}s=""print(o:gsub("()(.)",function(i,c)if u[c]then z={}l=#({o:sub(0,i):reverse():match("^(.)(.-)%1")})[2]:gsub(".",function(c)z[c]=(z[c]or 0)+1 return z[c]==1 and 1 or""end)return l>9 and"("..l..")"or l end u[#u+1]=c u[c]=#u s=s..c return #u>9 and"("..#u..")"or#u end).."#"..s)

Decryption implementations

Lua

I,i=io.read():match("(.-)#(.+)")s=""n=1 p=1 repeat V=I:sub(p):match("^%b()")v=(V and V:sub(2,-2)or I:sub(p,p))+1 p=p+(V and#V or 1)if v>n then s=s..(" "..i):sub(v,v)n=n+1 else u=""z={}s:reverse():gsub(".",function(c)z[c]=(z[c]or 0)+1 u=u..(z[c]==1 and c or"")end)s=s..u:sub(v,v)end until#I<p return s