Skip to content

Instantly share code, notes, and snippets.

@rsheldiii
Created June 27, 2012 00:27
Show Gist options
  • Save rsheldiii/3000444 to your computer and use it in GitHub Desktop.
Save rsheldiii/3000444 to your computer and use it in GitHub Desktop.
adfgvx brute force decryptor. decryption based on wordlist and caesar shift of the alphabet, using the fact that space will be the most prevalent letter
def encodingDict(scrambledAlphabet):
adfgvxIndex = ['A','D','F','G','V','X']
encodeDict = {}
for i in range(0,6):
for j in range(0,6):
encodeDict[scrambledAlphabet[i*6+j]] = adfgvxIndex[i]+adfgvxIndex[j]
return encodeDict
def DecryptOnSpace(encryptedString,codeWord):
#constructing then unsorting the columns of text
codeWord = [letter for letter in codeWord]
sortedCodeWord = codeWord[:]
sortedCodeWord.sort()
encryptedGrid = []
chunkLength = len(encryptedString) // len(codeWord)
for letter in sortedCodeWord:
encryptedGrid.append((letter,encryptedString[:chunkLength]))
encryptedString = encryptedString[chunkLength:]
sortedEncryptedGrid = []
#unsorting
for letter in codeWord:
for i in range(0,len(encryptedGrid)):
item = encryptedGrid[i]
if item[0] == letter:
sortedEncryptedGrid.append(encryptedGrid.pop(i)[1])
break
encryptedString = ""
#appending to one long string
for i in range(0,len(sortedEncryptedGrid[i])):
for j in range(0,len(sortedEncryptedGrid)):
encryptedString += sortedEncryptedGrid[j][i]
#guessing based on most prevalent 2-letter entry
freqDict = {}
for i in range(0,len(encryptedString),2):
target = encryptedString[i:i+2]
freqDict[target] = freqDict.get(target,0)+1
adfgvxCalc = {"A" : 0,"D" : 1,"F": 2, "G": 3, "V" : 4, "X" : 5}
bigkey,bigvalue = "",0
for key,value in freqDict.items():
if value > bigvalue:
bigvalue = value
bigkey = key
bigint = adfgvxCalc[bigkey[0]]*6+adfgvxCalc[bigkey[1]]
encodeDict = encodingDict(createCaesar(35-bigint))
encodeDict = {value:key for key,value in encodeDict.items()}#reverse
#actual decrypting
decryptedString = ""
for i in range(0,len(encryptedString),2):
target = encryptedString[i:i+2]
decryptedString += encodeDict[target]
return decryptedString
#Encrypt("Brake me out of jail on the 21st.","R3FLMX7KWQ69D4Y5NOZ STV2EH8AP1ICBGU0","PROGRAMMER")
#Decrypt("GADGVDGVFAAGVDAVGAXAGDGFXGGFDDXADXAAAGXFVGXGXGGVGFDVDDDFVVGVFGFAXFGGXF","R3FLMX7KWQ69D4Y5NOZ STV2EH8AP1ICBGU0","PROGRAMMER")
def createCaesar(num):
temp = "ABCDEFGHIKLMNOPQRSTUVWXYZ0123456789 "
return temp[num:]+temp[:num]
def guessDecrypt(encryptedString):
f = open("cryptastic","r")
allwords = []
allwordsdict= {}
for line in f:
line = line.rstrip("\n\r\t").upper()
if len(line)>2:
allwords.append(line)
allwordsdict[line.replace("J","I")] = 1
biggestcount = 0
biggesttext = ""
for word in allwords:
decrypted = DecryptOnSpace(encryptedString,word)
count = 0
for wordie in decrypted.split(" "):
count += allwordsdict.get(wordie,0)
if count > biggestcount:
biggestcount = count
biggesttext = decrypted
print(word)
print(count)
print(biggesttext)
print("\n")
guessDecrypt("VGVFFDXFAAVVXGAFAVAAGFAVAFAGVGXXAAVFGVAXGGVVGGXFGVGDVGXGVAGGGDXDVFGFAFVGGVAGGFGDGFAXGAGAXGVGAAGDGFXDXVAGVVAXGVGVGFVVAGXDVDGAGDGFXVGXAXGGXGVXGVVDGDGGXDADGGVGAVAGGDXDXVGGGVGXGFXFXFGVXXAFGDGXDAXXAGVFVVAVVGGVDVXAXAFDGVDXVAAVXAFVFGVAXVFADDAVFAFGVGVGFDVADVDVVXGXGGDVGXGVFGGAVADAFVFAGGFXXGFGVXFVFXAVFGFAFGVGFAVAAVFGFVAAVVVGDVAAXVGXDGDGGAGVDVGXDAXADGXGGADGFAFXGXVXVVVAFXXVFVVAAGVXXGGXADGAGFXFGFAGVFVVAFADXDGGGDGXAFADGDAFVDGFVGAGGXGFXDXGGFXXAGADXXAVXFXFAAXGVVAGVFVVVGVFDDVGDFVGXFXDXFDXVGVGAVXDDFAFVFDGVFGFGDGGVGXGVFAFGFVGVXVGVGXFVV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GVVVAGXGGDADAVADGDVDXXAGADGXXGGFVGADXFGFVDAGGDVFAGAFXGVGGVVVAFGXGXGGGFVGXGAGXAGDGAVFXDGXVGVVVGVGGVGVXDGDGXVXVDXDAVGDGDXGXGVDVVGFXFXXGDXFGDDDAVXDAAXAGXVFVVAGXDXGXGVFGFVAVXXFGGXFDVXAVDGDAGAGXXXFAXGXVDVFAGADDGXDGDGFVFXGGFAXDXGXVFGFAGGFVVXDGVGDGXGXGGVXGFVVXGVXXVVFVGGFVDGDXGAFGFXFVFAGGFAVADAVGGDDVFXFXGVFXGVXXGXGXGDGXAGXAFGGGGFVFVGAXADGDGVXVAFVFAGAFXGAGGDXXGDGGDGXVVFVDGFGGVAGGGDVXXFXDVVGFXVXVGDVVGFXVGGXFVFAXAFXVGDGXXDVGVGXGAAGGGDAFVFGGGFAGVVGVVXAXAFVDGGGFXGDXGVVVGGVFADGVAGADXDGFVGVGXGXDXAGFGGGGVGXFD")
@Bobavince
Copy link

Do you have the cryptastic file ?

GG 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment