Guy Torbet

Gambit Research Cipher

A solution to the Gambit Research Cipher puzzle

This was a particularly fun puzzle from Gambit Research.

158  94 202 194 104 138 118  60 205 196  96 208 183 109 211
194  90 210 191 104 204 201  25 196 197 107 126 201 104 202
204  98 204 189  25 210 190  94 126 157  90 203 184  98 210
118  92 198 183 101 202 187 103 197 187  39 126 166 101 195
183 108 195 118 108 195 196  93 126 207 104 211 200  25 209
197 101 211 202  98 205 196  25 191 196  93 126 153  79 126
202 104 126 191  92 191 196  92 205 186  94 158 189  90 203
184  98 210 200  94 209 187  90 208 185  97 140 185 104 203
118 106 211 197 109 199 196  96 126 200  94 196 187 107 195
196  92 195 144  25 142 142  44 196 134  93 193 138  41 142

After some digging into the page source, we can find the following JavaScript detailing how the cipher was generated.

// You're on the right path!
function scramble(message, a, b, c) {
  return message
    .split("")
    .map((chr, i) => {
      const code = chr.charCodeAt(0);
      switch (i % 3) {
        case 0:
          return (code + a) % 256;
        case 1:
          return (code + b) % 256;
        case 2:
          return (code + c) % 256;
      }
    })
    .join(" ");
}

We can brute-force all 256^3 combinations of a, b, and c until we find a string that contains a likely word. In this case, we can look for the word “gambit” in the descrambled text.

from itertools import product

def descramble(cipher: list[int], a: int, b: int, c: int) -> str:
    shifts = (a, b, c)
    return "".join(chr((value - shifts[i % 3]) % 256) for i, value in enumerate(cipher))

for a, b, c in product(range(256), repeat=3):
    text = descramble(cipher, a, b, c)
    if "gambit" in text.lower():
        print(f"{a=}, {b=}, {c=}")
        print(text, end="\n\n")

We get the following results:

a=86, b=25, c=94
HEllO, #onGraTulAtiOnsfoR sOlvIngthE GAmbIt ChaLleNge PLeaSe SenD yOursoLutIonanD C6 tO iCanCodE@gAmbItrEseArcH.cOm QuoTinG rEfeRenCe:08f0Dc40
--------------------------------------------------------------------------------
a=86, b=25, c=126
HELlO
      #OnGRaTUlATiONsFoRsOLvINgThEGAMbIT CHaLLeNGePLEaSE SEnDyOUrSoLUtIOnAnDC6tOiCAnCOdE gAMbITrESeARcHcOM QUoTInGrEFeREnCE:8F0DC4
--------------------------------------------------------------------------------
a=86, b=249, c=94
Hello, Congratulations for solving the Gambit challenge. Please send your solution and CV to icancode@gambitresearch.com quoting reference: 083f0dc400
--------------------------------------------------------------------------------
a=86, b=249, c=126
HeLlo
      COngRatUlaTioNs ForsoLviNg TheGaMbiT cHalLenGe.PlEasE sEndyoUr SolUtiOn AndCVtoicAncOde gaMbiTreSeaRchcoM qUotIngreFerEncE: 83F0dC40
--------------------------------------------------------------------------------
a=118, b=25, c=94
(ElLO,#oNGrATuLAtIOnSfOR SOlVInGtHE 'AmBItChALlENgE 0LeASeSeND YOuRsOLuTIoNaND #6 TO ICaNCoDE@GAmBItREsEArCH.COmQuOTiNG REfEReNCe0fDc0
--------------------------------------------------------------------------------
a=118, b=25, c=126
(ELLO
     #ONGRATULATIONSFORSOLVINGTHE'AMBITCHALLENGE0LEASESENDYOURSOLUTIONAND#6TOICANCODE GAMBITRESEARCHCOMQUOTINGREFERENCEFDC
--------------------------------------------------------------------------------
a=118, b=249, c=94
(elLo,CoNgrAtuLatIonS fOr SolVinG tHe 'amBitchAllEngE. 0leAseseNd YouR sOluTioN aNd #V To IcaNcoDe@GamBitResEarCh.ComquOtiNg RefEreNce 03fdc00
--------------------------------------------------------------------------------
a=118, b=249, c=126
(eLLo
     CONgRAtULaTIoNS FOrSoLViNG THe'aMBiTcHAlLEnGE.0lEAsEsENdYoUR SOlUTiON ANd#VToIcANcODe GaMBiTReSEaRChCoMqUOtINgReFErENcE 3FdC0

Which are all variations of the same sentence, but we can see that a=86, b=249, c=94 gives us the most readable version of the decoded message.